Java Reference
In-Depth Information
One approach to reading keyboard input is to read an entire line of input into a
variable of type String —for example, with the method nextLine of the Scanner
class—and then to use the StringTokenizer class to decompose the string in the
variable into words.
The class StringTokenizer is in the standard Java package (library) java.util .
To tell Java where to find the class StringTokenizer , any class or program that uses
the class StringTokenizer must contain the following (or something similar) at the
start of the file:
import
import java.util.StringTokenizer;
Perhaps the most common use of the StringTokenizer class is to decompose a
line of input. However, the StringTokenizer class can be used to decompose any
string. The following example illustrates a typical way that the class StringTokenizer
is used:
StringTokenizer wordFactory =
new StringTokenizer("A single word can be critical.");
while (wordFactory.hasMoreTokens())
{
System.out.println(wordFactory.nextToken());
}
This will produce the following output:
A
single
word
can
be
critical.
The constructor invocation
new StringTokenizer("A single word can be critical.")
produces a new object of the class StringTokenizer . The assignment statement
StringTokenizer wordFactory =
new StringTokenizer("A single word can be critical.");
gives this StringTokenizer object the name wordFactory . You may use any string
in place of "A single word can be critical." and any variable name in
place of wordFactory . The StringTokenizer object created in this way can be
used to produce the individual words in the string used as the argument to the
StringTokenizer constructor. These individual words are called tokens .
tokens
 
Search WWH ::




Custom Search