Results 1 to 2 of 2
  1. #1
    Landon is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default Looping through tokens

    Hi again,
    I'm currently stuck on a program. I have a string that is turned into tokens. I need to loop through the tokens and so the output will look something like this:
    Bob 45 34 29 1 87 total 197
    total Bob 98 34 23 23 total 178
    and so on. I know the code I have right now isn't close to being correct but I wanted to get the idea i had down quickly. I wanted to see if i'm on the right(ish) track or should abandon this method and go a different way? and also when I'm looping and trying to check what type the next token is how do I do that? Like so, if (S = word) or if (S = float).
    Thanks in advance
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class theLoop
    {
        public static void main(String[] args)
        {
            String S = "Bob 45 34 29 1 87    total      789    total Bob 98 34 23        23          fred 50 50 50 40 total 190";
            StringTokenizer StrTok = new StringTokenizer(S);
            
            while (StrTok.hasMoreTokens)
            {
                String S = StrTok.nextToken();
                int State == 1;
                
                //If statement for first state
                if (State = 1 && S = char)
                {
                    State == 2;
                }
                else
                {
                    System.out.println("Name is not the first object, program terminating");
                }
                
                //Statement for the second state
                if (State = 2 && S = char)
                {
                    //Combine with previous word to form name
                    State == 2;
                }
                else if (State == 2 && S = float)
                {
                    State == 3;
                }
                
                //Statement for the 3rd state
                if (State = 3 && S = float)
                {
                    //Add with previous number
                    State == 3;
                }
                else if (State = 3 && S = "total")
                {
                    State == 4;
                }
                else
                {
                    State == 5;
                }
                
                //Statement for the 4th state
                if (State = 4 && S = float)
                {
                    //Add all the previous numbers
                    //Print Name followed by numbers
                    //and total
                    State == 1;
                }
                
                else 
                {
                    //Keep previous total b/c it is part of a name
                    //Print name, numbers, and total again
                    State == 2;
                }
                
                if (State = 5)
                {
                    //Print name, number, and totals
                    //keep new word 
                    State == 2;
                }
                
                
                
        }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    hi Landon, your code if full of errors. please learn the basics like the difference between = and ==, otherwise you will never be able to wrote advanced code. a starting point is here Java Tutorials and then follow the links in the section Trails the Basics. said that here is some code that produce the output like

    Bob = word
    45 = int

    and so on. note that the number are all formatted as integer and not float.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class theLoop {
        public static void main(String[] args) {
            String input = "Bob 45 34 29 1 87    total      789    total Bob 98 34 23        23          fred 50 50 50 40 total 190";
            // first of all replace multiple spaces with a single one
            String s1 = input.replaceAll("\\b\\s{2,}\\b", " ");
            // the following println is only for checking the s1 string after the replaceAll
            System.out.println(s1);
            // split your string in tokens
            String[] s2 = s1.split(" ");
            for (String token : s2) {
                try {
                    Integer i = Integer.valueOf(token);
                    System.out.println(i + " = int ");
                    // if the valueOf throws a exception if the token was not a integer
                    // not very elegant but working
                } catch (IllegalArgumentException ex) {
                    // the IllegalArgumentException was thrown so the token is a word
                    System.out.println(token + " = word");
                }
            }
        }
    }

Similar Threads

  1. Indexing tokens
    By peliukasss in forum Lucene
    Replies: 4
    Last Post: 08-06-2010, 01:40 AM
  2. Getting tokens using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 09:11 AM
  3. tokens
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 12-02-2007, 11:30 PM
  4. How to use StringTokenizer for multiple tokens
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-29-2007, 09:38 AM
  5. tokens
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-25-2007, 02:39 AM

Tags for this Thread

Posting Permissions

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