File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes plz correct code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "plz correct code" Watch "plz correct code" New topic
Author

plz correct code

prathimaprasun rao
Ranch Hand

Joined: Jun 29, 2006
Posts: 55
Hello everyone,
I have to read a question from command line. Then I need to count the number of words and display the number of words as output. This is the code I have written. But I am not able to get the output. Cud anyone correct the code.

import java.io.*;
public class wordcount {
private static long countWords(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 ) throws IOException{
BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
if (args.length == 0) {
countWords("stdin");
} else {
for (int i = 0; i < args.length; i++) {
countWords(args[i]);
}
}
}
}


Eagerly waiting for reply.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Are you not allowed to use other methods in the String class? Starting in 1.4 there is a method that could do that in one line.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Your "countWords" method is returning a value, but your main() method is just ignoring it. main() should use, e.g., System.out.println() to actually display the result of calling the method.


[Jess in Action][AskingGoodQuestions]
prathimaprasun rao
Ranch Hand

Joined: Jun 29, 2006
Posts: 55
hello everyone,

could anyone clearly explain where the error is?

thanks in advance.
prathimaprasun rao
Ranch Hand

Joined: Jun 29, 2006
Posts: 55
Hello Keith Lynn

could you please write the code for me how to do this in a single line.
the main idea is i need to read one sentence from command line and display the number of words present in it

thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
could you please write the code for me how to do this in a single line

No. This is an assignment that is supposed to teach you something. How are you going to learn if someone else does the work for you?


Ping & DNS - my free Android networking tools app
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I agree with Ulf. If you read through the API for String, you will see several useful methods.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: plz correct code