If you're just talking about simple English strings like the example (and this is a beginner's homework assignment) then the StringTokenizer and String.split() methods will work just fine. But in real life, especially in languages like Japanese and Thai, extracting words from strings is a rather difficult task.
word counting can be done in easy way by using JAVA API or you can use your own algorithm like counting the character and if next character is space then increment the counter. you can check for other cases like if there is more than single space or string starts with a space or ends with more than one space.
Joined: Feb 03, 2005
A simple way to count tokens of strings:
[ December 09, 2006: Message edited by: Jesse Crockett ] [ December 09, 2006: Message edited by: Jesse Crockett ]
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
It is effectively deprecated (though not officially marked as such) and should really not be used in new code.