Results 1 to 1 of 1
  1. #1
    kgrant5116 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Creating a custom String Tokenizer

    I have been given a lab assignment where I am to write my own StringTokenizer class. I am having some trouble understanding the logic of how this is done. As my loop goes through the string one character at a time, it is supposed to determine if the character it is looking at is a delimiter or not. If it is a not delimiter then it looks at the character before it and if that character is a delimiter then it has found the start of a token and saves the index in an 'int' variable. Vice versa for the 'end' variable. I have been working on this for over a week and cannot come up with how this is done. The main contains 4 separate strings that each use this class to tokenize the string. I havnt even gotten the first one to work where it only has one space as the delimiter. Any suggestions on how I might be wrong in my logic?

    Java Code:
    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;
            numTokens = 0;
            String token;
            //String delim = d;
            for (int i = 1; i < str.length(); i++)
            {
                String t = str.substring(i, i + 1);
                String u = str.substring(i - 1, i);
    
                if (end != 0)
                {
                    if (!t.equals(d))
                    {
                        if (u.equals(d))
                        {
                            start = str.indexOf(t);
                        }
                    }
                }
                if (t.equals(d))
                {
                    if (!u.equals(d))
                    {
                        end = str.indexOf(t);
                        token = str.substring(start, end);
                        star[index++] = token;
                        numTokens++;
                    }
                }
              // System.out.println(token);
            }
        }
    
        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 Lab5
    {
    
        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());
        }
    Last edited by kgrant5116; 03-21-2011 at 03:16 PM.

Similar Threads

  1. Need help with string tokenizer
    By ShortIt in forum New To Java
    Replies: 1
    Last Post: 02-18-2011, 08:04 PM
  2. String Tokenizer help
    By GreenTea in forum New To Java
    Replies: 4
    Last Post: 10-30-2010, 03:44 AM
  3. String Tokenizer
    By sumaih in forum Java Gaming
    Replies: 2
    Last Post: 08-21-2010, 04:23 PM
  4. String Tokenizer
    By hussainian in forum Advanced Java
    Replies: 1
    Last Post: 03-16-2010, 09:58 AM
  5. String Tokenizer
    By viperlasson in forum New To Java
    Replies: 1
    Last Post: 03-09-2010, 02:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •