Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with string tokenizer in java

    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. Thanks
    Here is what i have now:
    Code:
    import java.util.*;
    import java.io.*;
    
    public class Calc
    {
    
      //prompts the user for input and calls necessary methods
      public static void main(String args[])
      {
        String cont = "";
    
        do
        {
          System.out.println("Please enter a numeric expression.");
          Calc.getInput();
    
          System.out.print("Do you want to enter another expression? (y/n): ");
          cont = Calc.getInput();
        }while(cont.equalsIgnoreCase("y"));
    
         System.out.println("Goodbye");
      }
    
       //gets a single input from the user, and returns it as a String
      static String getInput()
      {
        java.io.InputStreamReader input = new java.io.InputStreamReader(System.in);
        java.io.BufferedReader console = new java.io.BufferedReader(input);
    
        try
        {
          return console.readLine();
        }
        catch(java.io.IOException e)
        {
          System.err.println("Bad NEWS!!!");
          return "";
        }
      }
    }

  • #2
    New Coder
    Join Date
    Feb 2005
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    StringTokenizer is pretty straightforward. The idea is that you can separate a String by whitespace, or any other delimiter that you choose, for example a blank space, or a comma (blank space is default delimiter). Once you create a StringTokenizer object with a String, like for example:
    StringTokenizer foo = new StringTokenizer (s, ",");
    You can call foo.nextToken() to get the next chunk of String (delimited by a comma).
    For example:

    String s = "what the heck"
    StringTokenizer t = new StringTokenizer(s);
    while(t.hasMoreTokens()){
    System.out.println(t.nextToken());
    }

    will give the expected output:

    what
    the
    heck

    You need to make sure thast you know when there's no tokens left, since an exception will be thrown if you try to get the next token and it's not there.
    There are several algorithms you could use to solve the mathematical expression. I'd use a stack, but that's just me. At least you now know how to get each 'token' of the input. Read up on infix expressions, and how to solve them. It's pretty simple, once you think about it.


  •  

    Posting Permissions

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