What is a StringTokenizer?
In java, there is a way to break up a line of text into what are called
tokens. A token is a smaller piece of a string. This method of breaking up tokens
are come from the StringTokenizer class.
Let's say that we wanted to break up the following line of text into smaller pieces:
Here, the token we need is just a simple white space. So here is how to declare a StringTokeizer
object:
where in the above, name is an appropriate name for the tokenizer object; variable is the name
of a String variable you are using or even just a simple string; and token(s) is the token you
are specifying.
In the above definition, if the second argument of the constructor is not specified, the token,
by default, is a white space. If the token that is declared does not exist in the
String you are searching through, the entire string is returned to you.
Import statements
In order for the StringTokenizer class to be used, you must import
it from a certain library; here the util library.
An import statement always appears outside the class header line. So here is how the
StringTokenizer statement will look:
The above is specifically for the StringTokenizer class. There are many other libraries that can be
used in java. When the time is needed to describe these, we will.
Methods
Here are the most used functions in the StringTokenizer class.
boolean hasMoreTokens()
This method will return a boolean value that will represent if there are any more tokens
left in the line you are trying to tokenize. The true return will show that there is
at least 1 more token in the line.
int countTokens()
This method will simply return an integer representing the number of tokens in the String.
This method does not advance the position in the String.
String nextToken()
This method is the primary method of the class as it will get you the next piece of data in the
String. The position in the string is changed after this method is called.
Below is an example showing these above methods.
Example 1:
StringTokenizer example
Download source code here (Right click - Save Tagret As...)
This program will simply break up the String named s into 7 pieces, each on a new
line based on the println statement.