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 Target As...)
This program will simply break up the String named s into 7 pieces, each on a new
line based on the println statement.
Example 2:
StringTokenizer example with arrays
Download source code here (Right click - Save Target As...)
This program will take a String with multiple tokens and place each part in an array of Strings. It will then find the index of the largest String in the array.
The program first starts by declaring the StringTokenizer with 3 tokens, the plus sign, equals sign and minus sign. These are determined by looking at the String in the program.
The array we need is now determined to be the number of tokens in the String. Here, the countTokens() method will return 4 so in turn, the array will be of size 4.
The program then places each element in the String into the array making use of the nextToken() method in the while loop. Once complete, we need to then determine the index of the biggest word. This is simply done by comparing each element of the array with the current maximum element.
The output from the above program is:
The largest element is in index: 1