Results 1 to 2 of 2
  1. #1
    trill is offline Member
    Join Date
    Jul 2007
    Rep Power

    Default Help with string token

    Hi, I need to make a program that has the user enter an expression and solve the expression from left to right ignoring any order of operations.
    For example the expression: 2+5*2 = 14. I have everything done except for the solving of the expression. I dont know how to break up the inputed string. I know i need to use the string tokenizer but im not sure how.
    Can someone help point me in a direction.
    Here is what i have now:

    Java Code:
    import java.util.*;
    public class Calc
      //prompts the user for input and calls necessary methods
      public static void main(String args[])
        String cont = "";
          System.out.println("Please enter a numeric expression.");
          System.out.print("Do you want to enter another expression? (y/n): ");
          cont = Calc.getInput();
       //gets a single input from the user, and returns it as a String
      static String getInput()
      { input = new; console = new;
          return console.readLine();
        catch( e)
          System.err.println("Bad NEWS!!!");
          return "";

  2. #2
    levent Guest


    You don't need stringtokenizer here. Just read your line as a string. Then create a loop which will go over all characters of the string. Append each character to a temporary stringbuffer (stringbufferLeftExpression) until you find one of the operators. Then save operator type and in a similar way, find the right expression by appending characters to a temporary stringbuffer (stringbufferRightExpression). When you find the next operator, apply previous operation on stringbufferLeftExpression and stringbufferRightExpression and save the result to stringbufferLeftExpression. This loop goes like this until whole operation is finished.

    I try to explaing it without writing the code. Try it yourself if you understand it. If something is unclear let me know.

Similar Threads

  1. Syntax error on token "(", ; expected
    By baltimore in forum AWT / Swing
    Replies: 3
    Last Post: 10-28-2009, 01:19 AM
  2. Replies: 0
    Last Post: 11-20-2007, 05:59 PM
  3. Hard Token Management Framework 1.0_Beta1
    By levent in forum Java Software
    Replies: 0
    Last Post: 08-07-2007, 05:26 PM
  4. Replies: 1
    Last Post: 08-07-2007, 08:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 AM

Posting Permissions

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