SampleTokenizer sample

The SampleTokenizer Java™ class converts a string into a token based on a delimiter. This class is different from the StringTokenizer. This class returns an empty String if it encounters two instances of the delimiter together.

package com.ibm.ccd.api.samplecode;

import java.util.*;

public class SampleTokenizer
{ 

    String string;
    String delim;
    int currentIndex;
    int length;

    /**
     * Constructs the string to be converted
     *
     * @param 
     *     str, a string to be parsed
     * @param 
     *  delim, the char delimiter
     */

    public SampleTokenizer(String string, char delim)
    {
        this.delim = delim + "";
        this.string = string;
        this.currentIndex = 0;
        this.length = string.length();
    }

    /**
     * Constructs the string to be converted
     *
     * @param 
     *     str, the string to be parsed
     * @param 
     *    delim, the string delimiter
     */
    public SampleTokenizer(String string, String delim)
    {
        this.delim = delim;
        this.string = string;
        this.currentIndex = 0;
        this.length = string.length();
    }


    public boolean hasMoreTokens()
    {
        return (currentIndex <= length);
    }


    public int countTokens()
    {
        int count = 1;
        int tempIndex = this.currentIndex;

        if (currentIndex >= length)
            return 0;

        while (((tempIndex = this.string.indexOf(delim, tempIndex)) != -1)
               && (tempIndex < this.length))
        {
            tempIndex  = tempIndex + delim.length();
            count++;
        }

        return count;
    }

    public String nextToken()
    {
        int tempIndex = this.string.indexOf(delim, currentIndex);
        if (tempIndex == -1)
            tempIndex = length;
        String str = this.string.substring(currentIndex, tempIndex);
        currentIndex = tempIndex + delim.length();
        return str;
    }

    public Object nextElement()
    {
        return nextToken();
    }

    public static void main(String [] args)
    {
        String stringToBeParsed = "a,b,,c,d";
        SampleTokenizer st = new SampleTokenizer(stringToBeParsed, ",");
        while(st.hasMoreTokens())
        {
            System.out.println("Token = ["  + st.nextToken() + "]");
        }
    }





}