1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Java: String defined?

Discussion in 'Software Development' started by Gibbs, Feb 15, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. Gibbs

    Gibbs Thread Starter

    Joined:
    Feb 28, 2005
    Messages:
    609
    Hey,

    I've been a little confused over the past few hours. I'm following a tutorial and everything has gone fine up until the past few steps. Is the String "fname" defined? It's confusing as the program doesn't do anything. Even when I add "fname = "test.txt" or something similar nothing is read.

    Any help appreciated.

    Heres the code:

    Code:
    import java.io.*;
    
    public class WordCount1
    {
    	private static void linecount(String fname, BufferedReader in) throws IOException
    	{
    		long numChar = 0;
    		long numLine = 0;
    		long numWords = 0;
    		String line;
    
    		do
    		{
    			line = in.readLine();
    
    			if (line != null)
    			{
    				numChar += line.length();
    				numWords += wordcount(line);
    				numLine++;
    			}
    		}
    
    		while (line != null);
    			System.out.println("File name: " +fname);
    			System.out.println("Number of characters: " + numChar);
    			System.out.println("Number of lines: " + numLine);
    			System.out.println("Number of words: " + numWords);
    	}
    
    	public static void linecount(String fileName)
    	{
    		BufferedReader in = null;
    
    		try
    		{
    			FileReader fileReader = new FileReader(fileName);
    			in = new BufferedReader(fileReader);
    			linecount(fileName,in);
    		}
    
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    
    	public static long wordcount(String line)
    	{
    		long numWords = 0;
    		int index = 0;
    		boolean prevWhiteSpace = true;
    
    		while (index < line.length())
    		{
    			char c = line.charAt(index++);
    			boolean currWhiteSpace = Character.isWhitespace(c);
    			
    			if (prevWhiteSpace && !currWhiteSpace)
    			{
    				numWords++;
    			}
    		
    			prevWhiteSpace = currWhiteSpace;
    		}
    	return numWords;
    	}
    
    	public static void main(String[] args)
    	{
    		long numChar = 0;
    		long numLine = 0;
    		String line;
    
    		try
    		{
    			if (args.length == 0)
    			{
    				BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    				line = in.readLine();
    				numChar = line.length();
    
    				if (numChar != 0)
    				{
    					numLine = 1;
    				}
    				
    				System.out.println("Number of characters: " + numChar);
    				System.out.println("Number of words: " + wordcount(line));
    				System.out.println("Number of lines: " + numLine);
    			}
    
    			else
    			{
    				for(int i = 0; i < args.length; i++)
    				{
    					linecount(args[i]);
    				}
    			}
    		}
    		
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi Gibbs,

    I'm having a look.
     
  3. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    The program works nicely.

    What it does ?
    ***************
    When file paths are passed as arguments, the application counts the number of characters, lines and words for
    each file referred.
    When no argument is passed, the console displays an input prompt : the user inserts whatever he wants, as soon he hits the enter key,
    the program counts the number of characters and words inserted.

    1) Test with passing the file path of the files I created, text.txt and text1.txt

    2) Test without passing any argument : I typed the sentence 'This is a test without passing arguments'

    ;)

    Edit :
    Also, the real point where the program starts is the main(String[] args) method.

    If you look inside, the first test made is : if (args.length == 0) :
    if no argument is passed, args.length will be equal to zero.
     
  4. Gibbs

    Gibbs Thread Starter

    Joined:
    Feb 28, 2005
    Messages:
    609
    Edit: Ah I've been a bit stupid. Thanks for your explanations and time, it's helped alot! Thank you!!!
     
  5. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    What IDE are you using ? JBuilder, Eclipse, NetBeans ?

    BTW, switch in mode console :
    - in your home directory, you may create a symbolic link from your java.bin,
    - you store a copy of WordCount1.class,
    - touch and vi two files you call test1, test2,
    - then launch : .java.bin WordCount1.class test1 test2
    or
    launch : .java.bin WordCount1.class

    In the first launch, you have two arguments : your files will be scanned and in the second launch, there's no argument : you'll get a prompt and you insert whatever you want.
     
  6. Gibbs

    Gibbs Thread Starter

    Joined:
    Feb 28, 2005
    Messages:
    609
    I'm using the Text Editor gedit. Can you recommend anything good?
     
  7. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    NetBeans, it works for Linux. You may download it from this page :

    http://www.netbeans.info/downloads/all.php?b_id=2323

    Really great IDE for free. I develop all my projects with it.
     

    Attached Files:

  8. Gibbs

    Gibbs Thread Starter

    Joined:
    Feb 28, 2005
    Messages:
    609
    Thanks for your help. I'm downloading NetBeans right now. Learning Java is fun and things are starting to fall into place. Cheers for your tips and info :)
     
  9. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    You're welcome ! :)
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/544344

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice