// StringTokenizer example

public class Tokenizer {

    public static void main(String[] args) {

        // Initialize string
        String line = "This is a line with lots of tokens on it!";

        // Call example code
        tokenExample(line);
    }

    private static void tokenExample(String s) {

        // Instantiate object
        StringTokenizer tokenizer = new StringTokenizer(s);

        // Count tokens
        int numberTokens = tokenizer.countTokens();
        System.out.println("Number of tokens: " + numberTokens);
        
        // Enumerate tokens
        int count = 0;
        while (tokenizer.hasMoreTokens()) {
            System.out.println(count + ") " + tokenizer.nextToken());
            count++;
        }
    }
}

© 2013 CS160 Colorado State University. All Rights Reserved.