Title: StringTokenizer
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

StringTokenizer

When you need to break a string apart into pieces, the StringTokenizer can simplify your life.

I would recommend that you compile and execute the following program to get an idea what this class does for you. In effect, it returns words in a string where delimiters can be defined. The different StringTokenizer constructors affect how the delimiters are defined. See the description of the Constructors below.

The StringTokenizer is a class in the package java.util. The StringTokenizer is derived from Enumeration which is an older form of an Iterator. We will encounter this concept in detail when we cover Collection classes. The 3 methods we are demonstrating in the following example are:

1. countTokens - the number of tokens in the parsed string
2. hasMoreElements - returns true as long as there are more tokens
3. nextElement - returns the next token as an Object class
4. nextToken - returns the next token as a String

StringTokenizer Constructors:

StringTokenizer(String) - default delimiter definition of just white space (space, tab, newlines)

StringTokenizer(String, delimString) - the second parameter contains all of the characters that act as delimiters

StringTokenizer(String, delimString, true) - The third parameter set to true indicates we want to get the delimiters returned as tokens as they are recognized.


import java.util.*;
class Tokens
{
   public static void main(String[] args)
   {
        String str = "Oats, Barley, and Wheat are examples of Grain. Apples and Pears are examples of Fruit.";


        System.out.println("Using StringTokenizer(String)");
        System.out.println(" -------------- ");

        StringTokenizer parse = new StringTokenizer(str);
        System.out.println("Number of tokens = " + parse.countTokens());
        while (parse.hasMoreElements())
            System.out.println(parse.nextElement());

        System.out.println("*******************************************");
        System.out.println("Using StringTokenizer(String, DelimString) ");
        System.out.println(" -------------- ");

        parse = new StringTokenizer(str, " ,.");
        System.out.println("Number of tokens = " + parse.countTokens());
        while (parse.hasMoreElements())
            System.out.println(parse.nextElement());

        System.out.println("*******************************************");
        System.out.println("Using StringTokenizer(String, DelimString, true) ");
        System.out.println(" -------------- ");

        parse = new StringTokenizer(str, " ,.", true);
        System.out.println("Number of tokens = " + parse.countTokens());
        while (parse.hasMoreElements())
            System.out.println(parse.nextToken());

   }
}


Exercises:

1. Use the StringTokenizer class to solve the Work Gathering Exercise
2. Write your own StringTokenizer Class using String classes only. Use your new class to solve the Work Gathering Exercise

Last Updated: September 27, 2002 12:48 PM