Java Reference
In-Depth Information
The method nextToken returns the first token (word) when it is invoked for the
first time, returns the second token when it is invoked the second time, and so forth. If
your code invokes nextToken after it has returned all the tokens in its string, then your
program will halt and issue an error message.
The method hasMoreTokens is a method that returns a value of type boolean ; that
is, it returns either true or false . Thus, an invocation of hasMoreTokens , such as the
following
nextToken
hasMore
Tokens
wordFactory.hasMoreTokens()
is a Boolean expression, and so it can be used to control a while loop. The method
hasMoreTokens returns true as long as nextToken has not yet returned all the tokens
in the string, and it returns false after the method nextToken has returned all the
tokens in the string.
When the constructor for StringTokenizer is used with a single argument, as in
the preceding example, the tokens are substrings of nonwhitespace characters, and the
whitespace characters are used as the separators for the tokens. Any string of one or
more whitespace characters is considered a separator. Thus, in the preceding example,
the last token produced by the method nextToken is "critical." including the
period. This is because the period is not a whitespace character and so is not a separator.
You can specify your own set of separator characters. When you create your
own set of separator characters, you give a second argument to the constructor for
StringTokenizer . The second argument is a string consisting of all the separator
characters. Thus, if you want your separators to consist of the blank, the new-line
character, the period, and the comma, you could proceed as in the following example:
choosing
delimeters
StringTokenizer wordfactory2 =
new StringTokenizer("Give me the word, my friend.", " \n.,");
while (wordfactory2.hasMoreTokens())
{
System.out.println(wordfactory2.nextToken());
}
This will produce the output
Give
me
the
word
my
friend
 
Search WWH ::




Custom Search