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.