Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Looping through Tokens

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Looping through Tokens

    Hi, I have a program were I have a string of tokens. I need to determine which tokens are words and which are number. I don't know the method to do this. If there a parseFloat or a parseWord or parseChar of somekind that I could use? Here is the code, hopefully this explains what I'm going for better than how I explained it. Also I know they are all Ints but i need it to work for floating point also.
    import java.io.*;
    import java.util.*;
     
    public class theLoop {
        public static void main(String[] args) {
            String newStr = "Bob 45 34 29 1 87    total      789    total Bob 98 34 23        23          fred 50 50 50 40 total 190";
     
            //Create new String tokenizer
            StringTokenizer StrTok = new StringTokenizer(newStr);
     
            //Set State to 1
            int State = 1;
     
            //Set Loop to run while there are more tokens
            while(StrTok.hasMoreTokens)
            {
                //Make new String for tokens
                String S = StrTok.nextToken();
     
                //Loop for first state
                if (State == 1)
                {
                    if (S = //??? a word ???)
                    {
                        //Keep the word????, and proceed to next step
                        State = 2;
                    }
                }
     
                //Loop for second state
                if (State == 2)
                {
                    if (S =  //??? a word ???)
                    {
                        //?? Add word to previous word ??
                        State = 2;
                    }
                    else if (State = //?? a floating-point //??)
                    {
                        System.out.println(//the name from before);
                        //?? Remember number
                        State = 3;
                    }
                }
                //and so on
    Thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,330
    Thanks
    181
    Thanked 828 Times in 771 Posts
    Blog Entries
    5

    Default Re: Looping through Tokens

    Quite a few ways to do this...
    string.matches("[0-9]*[\\.]*[0-9]+")
    Uses a regular expression which should validate an integer or floating point number. You could do the same thing for words:
    string.matches("[a-zA-Z]+")
    But it can get more complex if you want to include things other than letters. You use the first method to validate for a number, then if it doesn't validate assume its a word.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Viking N7 (September 12th, 2010)

  4. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Looping through Tokens

    Thanks, I'll give it a shot.

Similar Threads

  1. Whats wrong with my looping?
    By argie123sky in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 21st, 2010, 08:13 PM
  2. Looping Question (newbie)
    By xdrechsler in forum Loops & Control Statements
    Replies: 13
    Last Post: July 19th, 2010, 09:12 PM
  3. Looping Question
    By miss confused in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 30th, 2010, 12:46 PM
  4. Not Looping? (do - while) bad execution!
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 23rd, 2009, 07:51 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM

Tags for this Thread