Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I need to implement my own version of the StringTokenizer class. It would I'd like to know if I'm at least heading in the right direction with my isDelim and my first ST method, as they are the only ones I want to work with.

What I have tried:

Here are the 2 methods I want to work with:
Java
public ST(String s, String d)
    {
        star = new String[50];
        String str = s;
        int start = 0;
        int end =0;
        //String delim = d; not sure where to put it...
        String token;
        numTokens = 0;
 
        for(int i=1; i<str.length(); i++)        
        {
            String c1 = str.substring(i, i+1);
            String c2 = str.substring(i-1, i);
 
            if(c1 == " ")
            {
                if(c2 != " ")
                {
                    start = i+2;
                }
            }
            if(c1 != " ")
            {
                if(c2 == " ")
                {
                    end = i;
                }
            }
 
            if (end < start)
            {
                end = str.length();
            }
            star[numtokens] = str.substring(start,end);
            System.out.println("[" + star[numtokens] + "]");
            numtokens ++;
       }
    }

public boolean isDelim(String s)
    {
        String delim = s;
 
        return delim.substring(0, 1).equals(" ");
    }


And here's everything together:
Java
class ST
{
    String star[];
    int numTokens;
    int index = 0;
 
    public ST(String s, String d)
    {
        star = new String[50];
        String str = s;
        int start = 0;
        int end =0;
        //String delim = d; not sure where to put it...
        String token;
        numTokens = 0;
 
        for(int i=1; i<str.length(); i++)        
        {
            String c1 = str.substring(i, i+1);
            String c2 = str.substring(i-1, i);
 
            if(c1 == " ")
            {
                if(c2 != " ")
                {
                    start = i+2;
                }
            }
            if(c1 != " ")
            {
                if(c2 == " ")
                {
                    end = i;
                }
            }
 
            if (end < start)
            {
                end = str.length();
            }
            star[numtokens] = str.substring(start,end);
            System.out.println("[" + star[numtokens] + "]");
            numtokens ++;
       }
    }

    public boolean isDelim(String s)
    {
        String delim = s;
 
        return delim.substring(0, 1).equals(" ");
    }
 
    public ST(String s)
    {
        this(s, " ");
    }
 
    public int countTokens()
    {
        return (numTokens - index);
    }
 
    public boolean hasMoreTokens()
    {
        return (numTokens > index);
    }
 
    public String nextToken()
    {
        return (star[index++]);
    }
}

public class Token
{
    public static void main(String argv[])
    {
        String str;

    //1)
        str = "Hello world";
        ST stok= new ST(str);
        
        System.out.println(str);

        while (stok.hasMoreTokens())
        {
            System.out.println("#tokens = " + stok.countTokens());
            System.out.println("token: [" + stok.nextToken() + "]");
        }
        System.out.println("#tokens = " + stok.countTokens());
        //System.out.println("token: " + stok.nextToken());
        System.out.println("\n\n");

    //2)
        str = "    Hello    world   ";
        stok= new ST(str);
        
        System.out.println(str);

        while (stok.hasMoreTokens())
        {
            System.out.println("#tokens = " + stok.countTokens());
            System.out.println("token: [" + stok.nextToken() + "]");
        }
        System.out.println("#tokens = " + stok.countTokens());
        System.out.println("\n\n");

    //3)
        str = "root:x:0:0:root:/root:/bin/bash";
        stok = new ST(str, ":");

        System.out.println(str);


        int n = stok.countTokens();
        System.out.println("#tokens = " + n);

        for (int i=0; i<n; i++)
        {
            System.out.println("token [" + stok.nextToken() + "]");
        }

        //System.out.println("username = " + stok.nextToken());
        //System.out.println("password = " + stok.nextToken());
        //System.out.println("userid   = " + stok.nextToken());
        //System.out.println("groupid  = " + stok.nextToken());
        //System.out.println("comment  = " + stok.nextToken());
        //System.out.println("home dir = " + stok.nextToken());
        //System.out.println("shell    = " + stok.nextToken());
        //System.out.println("\n\n");

    //4)
        str = "Hello-world.It is!a nice day,today";
        stok= new ST(str,"-.!, ");
        
        System.out.println(str);

        while (stok.hasMoreTokens())
        {
            System.out.println("#tokens = " + stok.countTokens());
            System.out.println("token: [" + stok.nextToken() + "]");
        }
        System.out.println("#tokens = " + stok.countTokens());
    }
}
Posted
Comments
Richard MacCutchan 23-Jun-16 12:58pm    
The only way to be sure is to run the program against some test data and check the results.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900