More than one Delimiter Works
Tokens can be separated by more than one delimiter.
There are several spaces separating the tokens in the example.
Here are some methods of the class:
| StringBuffer methods |
| int countTokens() | return the number of tokens remaining. |
| boolean hasMoreTokens() | return true if there are more tokens. |
| String nextToken() | return the next token. |
The countTokens() method will start with the total number of
tokens the string has been broken into.
Each time nextToken() is called, one token (starting with the
first) is consumed, and countTokens() will report one less
token.
Here is the tiny program, expanded to use two of these methods.
import java.util.* ;
public class TokenTester
{
public static void main ( String[] args )
{
StringTokenizer tok;
if ( args.length > 0 )
{
tok = new StringTokenizer( args[0] );
while ( tok.hasMoreTokens() )
System.out.println( tok.nextToken() );
}
}
}
|