# Solved: Java: String defined?



## Gibbs (Feb 28, 2005)

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:


```
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();
		}
	}
}
```


----------



## Chicon (Jul 29, 2004)

Hi Gibbs,

I'm having a look.


----------



## Chicon (Jul 29, 2004)

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



Result 1 said:


> File name: text.txt
> Number of characters: 35
> Number of lines: 4
> Number of words: 5
> ...


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



Result 2 said:


> this is a test without passing arguments
> Number of characters: 40
> Number of words: 7
> Number of lines: 1




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.


----------



## Gibbs (Feb 28, 2005)

Edit: Ah I've been a bit stupid. Thanks for your explanations and time, it's helped alot! Thank you!!!


----------



## Chicon (Jul 29, 2004)

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.


----------



## Gibbs (Feb 28, 2005)

I'm using the Text Editor gedit. Can you recommend anything good?


----------



## Chicon (Jul 29, 2004)

Gibbs said:


> I'm using the Text Editor gedit. Can you recommend anything good?


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.


----------



## Gibbs (Feb 28, 2005)

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


----------



## Chicon (Jul 29, 2004)

You're welcome !


----------

