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 2 of 2

Thread: Streamtokenzier help

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

    Default Streamtokenzier help

    Im trying to apply the streamtokenzier on this string ex: 10+5 however it only recognizes 10 and 5 as numbers but it doesn't recognize the + as word does anybody know why? I will using it later to parse mathematical equations

    tokenizer.eolIsSignificant(true);
     
    ......
     
    try
    {
      while (tokenizer.nextToken() != StreamTokenizer.TT_EOF)
     {
     
         if (tokenizer.ttype == StreamTokenizer.TT_NUMBER)
        {
     
            System.out.println("number is :" tokenizer.nval "\n");
         }
     
        else if (tokenizer.ttype == StreamTokenizer.TT_WORD){
     
               System.out.println("word is :" tokenizer.sval + "\n");
        }
      }
     
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    Output in netbeans 6.8:

    number is :10.0

    number is :5.0
    Last edited by helloworld922; January 24th, 2010 at 04:20 PM.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Streamtokenzier help

    I believe this is because by default StreamTokenizer only allows word which have characters that are letters. You can change them by using the wordChars() method. If you are going to do something like parsing in an expression, though, I would strongly recommend using regular expressions.