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

Thread: Need Help with StringTokenizer.

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need Help with StringTokenizer.

    I am having trouble with StringTokenizer. I have an input file that has an infix expressions followed by
    a set of numbers which are the values for the variables

    A + B * C
    1 3 5 2 2 2

    The goal is to take the infix and convert it to postfix. Then you will take the postfix and using the numbers evaluate the postfix expression. I have most of the methods completed to do the conversions and evaluations. My problem is getting the data out of the files.

    I need to read the infix, print to an output file the infix, the postfix conversion, the values for the variables and the evaluated expression value.

    I have my stacks, & queues set up. I just need help getting the data from the file.
    StringTokenizer gives me tokens in strings but, I need to get characters and ints.
    This is only a snippet of my full code but, it compiles but will not run.

    Can anyone help with my use of StringTokenizer?


    while(inFile.hasNext())
    {
     
           line = inFile.nextLine();
           st = new StringTokenizer(line);
          while(st.hasMoreTokens())
          {
             test = st.nextToken();
             //ops = st.nextToken.charAt(0);
             numbers = Integer.parseInt((st.nextToken()));
     
          }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with StringTokenizer.

    help with my use of StringTokenizer?
    Most files contain text data that comes into a program as a String. It is up to the program to convert the String into the type of data that you want.
    The Integer, Float and Double classes have parse... methods that convert String data to int, float and double data. The String class has methods to access the contents of a String as char values.
    The Character class has methods for determining what kind of data is in a char: digit, letter, whitespace, etc

    Also look at the StreamTokenizer class. It has another set of methods that may be useful for parsing input.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with StringTokenizer.

    I understand this. I can not figure out how to go through the file.
    the way I wrote the while loop
    test = st.nextToken();

    grabs the next token which is A


    numbers = Integer.parseInt((st.nextToken()));
    trys to grab "+" which is not an int then crashes.

    I am not sure how to process the file to get what I want. Do I need another loop in there?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need Help with StringTokenizer.

    You are correct, '+' is not an int. So why are you trying to parse it into an int?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with StringTokenizer.

    Im not trying to parse it into an int. The code is wrong. I am asking what I need to do to read the first line which will be an expression.

    Then read the second line which are numbers and parse those to ints and continue till the end the file.

    The file is an expression on one line then numbers on the next.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need Help with StringTokenizer.

    Read line from file then use the split method which is preferred to StringTokenizer.
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with StringTokenizer.

    Im trying to do this but I am a still having problems because I still have strings. I need the infix expression to be in a character array to pass to my methods and the numbers to be integers to pass to my methods.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need Help with StringTokenizer.

    charAt
    Improving the world one idiot at a time!

Similar Threads

  1. FractionCalculator StringTokenizer with parsing DUE IN 4 HOURS
    By DUE IN 4 HOURS in forum What's Wrong With My Code?
    Replies: 27
    Last Post: November 3rd, 2011, 08:15 AM
  2. StringTokenizer problem
    By daniel_wu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 31st, 2010, 03:45 AM
  3. StringTokenizer error
    By mjpam in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 21st, 2010, 05:19 PM