Using the StringTokenizer class

The StringTokenizer class is found in the java.util package. It provides more flexibility than the StreamTokenizer class and is designed to handle strings from any source. The class' constructor accepts the string to be tokenized as its parameter and uses the nextToken method to return the token. The hasMoreTokens method returns true if more tokens exist in the input stream. This is illustrated in the following sequence:

StringTokenizerst = new StringTokenizer("Let's pause, and "
+ "then reflect."); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }

When executed, we get the following output:

Let's
pause,
and
then
reflect.

The constructor is overloaded, allowing the delimiters to be specified and whether the delimiters should be returned as a token.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset