This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
A quick and easy way is to use StringTokenizer with one whitespace " " as the delimiter. Calling countTokens() will give you the number of words.
e.g.
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
Starting with JDK 1.4, the String class has a method called split() that will split the String into an array of tokens using a regex as delimiter.
Luciano Mantuaneli
Greenhorn
Joined: Dec 04, 2006
Posts: 13
posted
0
It has to be pseudo code? Because I know a solution using features too specific of Java API. Can't see how to express it in pseudo code... Let me try:
If you want to see the code, let us know!
[]'s<br />Mantu<br /> <br />Sorry for my poor english...<br /> <br /><i>Time after time we lose sight of the way. Our causes cant see their effects</i> - Neil Peart
Luciano Mantuaneli
Greenhorn
Joined: Dec 04, 2006
Posts: 13
posted
0
Originally posted by Keith Lynn: Starting with JDK 1.4, the String class has a method called split() that will split the String into an array of tokens using a regex as delimiter.
This approach can be very tricky: Depending on your regex, you may have distorted values [ December 04, 2006: Message edited by: Luciano Mantuaneli ]
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.
Originally posted by Luciano Mantuaneli: If you want to see the code, let us know!
Luciano, please don't post the complete solution - that way Jalli will not learn anything. Have you read the description of the beginner forum? It says:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Luciano Mantuaneli
Greenhorn
Joined: Dec 04, 2006
Posts: 13
posted
0
Originally posted by Jesper Young:
Luciano, please don't post the complete solution - that way Jalli will not learn anything. Have you read the description of the beginner forum? It says:
Got it!
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
Originally posted by Kaydell Leavitt: What if the String has other white-space in it, such as a non-breaking space, a tab, a carriage-return, a linefeed, or a formfeed?
Are there other kinds of whitespace?
-- Kaydell
\s mathches A whitespace character: [ \t\n\x0B\f\r].
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.
Jesse Crockett
Ranch Hand
Joined: Feb 03, 2005
Posts: 129
posted
0
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.