Word Count in Java - So simple, that you'll laugh at it :)

What is Word Count? How could i do it in Java?


Word count is nothing but, the no. of words in a given string. So, let us think very small about it. Usually a word contains a space before and after it. That is how we can recognize a word. The user gives a String as input. Now, we will simply need to split the string into words, this can easily be done by calling the split() method in the String class by passing a space as parameter. Let us take a look at the following example.

The Example


import java.util.*;
class WordCount
{
public static void main(String args[])
{
// Create Scanner object
Scanner s=new Scanner(System.in);

// Read a string
String st=s.nextLine();

// Split string with space
String words[]=st.trim().split(" ");

System.out.println("No. of words "+word.length);

}
}
---------------------------------
Output
---------------------------------
This is sample string.
No. of words 4

Explanation


st.trim().split(" "): The trim() method in the String class cuts the starting and the ending spaces. If this is not done here and if the user gives " This is sample string. " as input, then the no. of words will be displayed as 5 which is not really so. To avoid this problem, the given string is first trimmed and then splitted by a space. As said earlier, a word is nothing but the one that prefixes and suffixes a space. So, space is used as an argument for the split() method in the String class.

Why nextLine(): If you write, the next() method instead of nextLine(), then the input is just taken and the space will be neglected, so this results in abnormality in the program, it just returns that there is only one word, no matter how many words the input string has. So, to avoid this we used the nextLine() method which considers space too.

More effective way of doing


What if string contains more than one space, one after the other? Even simpler. You do not need to write all the code. Just erase all the code that i have written above and see this.

new StringTokenizer(st).countTokens();

Isn't that simple? It will be. 


Posted by at on

Tags: java.lang, String Handling,