Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java
public class Lab6
{

    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("\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);

       // 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());
    }
}
class ST
{
    String star[];
    int numtokens = 0;
    int index=0;

    public ST(String s, String d)
    {
        star = new String[50];
        String str = s;
        int start = 0;
        int end =0;
        int u = str.indexOf(' ');
        String c0;

        for(int i=0; i<str.length();>        {
            c0 = str.substring(i,i+2);
            System.out.println("[" + c0 +"]");

            String c1 = c0.substring(0,1);
            String c2 = c0.substring(1,2);
            System.out.println("[" + c1 + "]");
            System.out.println("[" + c2 + "]");

            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 ST(String s)
    {
        this(s, " ");
    }

    public int countTokens()
    {
        return(numtokens - index);
    }
  public boolean hasMoreTokens()
    {
        return (index < numtokens);
    }

    public String nextToken()
    {
        return star[index++];
    }
}
Posted
Updated 30-Jun-15 14:37pm
v2
Comments
Matt T Heffron 30-Jun-15 20:38pm    
Is there a specific question here?
This looks like a "code dump"
Where are you having difficulty?
Richard MacCutchan 1-Jul-15 4:08am    
Too much code in your constructor, you should move most of that to the getToken method. You also have lots of values hardcoded for substring calls, which are likely to cause problems. You also ignore the second parameter value in the constructor which is, presumably, supposed to be the token delimiters.

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