Solved: Java: String defined?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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

Result 1 said:
File name: text.txt
Number of characters: 35
Number of lines: 4
Number of words: 5
File name: text1.txt
Number of characters: 22
Number of lines: 3
Number of words: 4
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

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!!!
 
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.
 

Gibbs

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

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 :)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Top