java help

Discuss all aspects of programming here.

Moderator: The Mod Squad

java help

Postby cwl157 » Fri Feb 11, 2005 12:48 pm

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: Select all
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 "";
    }
  }
}
cwl157
Black Belt
Black Belt
 
Posts: 851
Joined: Mon Apr 12, 2004 2:32 pm
Location: Chicago

Postby Roach412 » Fri Feb 11, 2005 1:33 pm

just use the same kind of logic as this example shows: http://www.koders.com/java/fid48C39989D ... 68580.aspx

it's a lot of code to go through...so be warned.

basically, break it into little pieces. find what pieces do what (numbers vs. operators) and shove it to the correct functions.

-Roach
Lian Li Lancool First Knight Series PC-K59W
Intel Core i7-960 Bloomfield 3.2GHz
EVGA X58 FTW3 132-GT-E768-TR
EVGA GeForce GTX 1070 FTW GAMING ACX 3.0
G.SKILL Ripjaws Series 12GB (3 x 4GB)
Crucial M4 128gb SATAIII SSD x2
Crucial M4 256gb SATAIII SSD
OCZ Saber 1000 960GB Enterprise SSD
Corsair Professional Series HX850 PSU
Dell UltraSharp U2713HM 27" w/drop ceiling mount
Logitech G930 7.1 Headset
DBPOWER RGB LED Keyboard
Logitech G402 Hyperion Fury
Roach412
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2714
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Postby cwl157 » Fri Feb 11, 2005 2:23 pm

ok now it will do all the opperations that i need it to do. But i still dont know how to check to see which tokens are numbers to know what the operation needs to be aplied to. I set num1 and num2 to values just to see if the string tokenizer would recongnize the different operations and it does. So now how do i make it so it recognizes the different numbers and performs the operations on the numbers the user enters.
Here is what i have i might have to change everything cause i dont know if it can be done like this:
Code: Select all
import java.util.*;
import java.io.*;

public class Calc1
{

  //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.");
       StringTokenizer exp = new StringTokenizer(Calc.getInput(), "+-*/%", true);
       //System.out.println(exp.countTokens());
       int result = Calc1.performOperation(exp);
       System.out.println(result);

      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 "";
    }
  }

  static int performOperation (StringTokenizer exp)
  {
    int num1 = 1;
    int num2 = 3;
    int result = 0;
    String check = "";

    while(exp.hasMoreTokens())
    {
      check = exp.nextToken();
      if(check.equals("+"))
      {
        result = num1 + num2;
      }

      else if(check.equals("-"))
      {
        result = num1 - num2;
      }

      else if(check.equals("*"))
      {
        result = num1 * num2;
      }

      else if(check.equals("/"))
      {
        result = num1 / num2;
      }

      else if(check.equals("%"))
      {
        result = num1 % num2;
      }
    }
    return result;
  }
}
cwl157
Black Belt
Black Belt
 
Posts: 851
Joined: Mon Apr 12, 2004 2:32 pm
Location: Chicago


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests