The Mother Board

Motherboards.org forums. Free tech support, motherboard ID, and more.
It is currently Fri Aug 17, 2018 8:49 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: java help
PostPosted: Fri Feb 11, 2005 12:48 pm 
Offline
Black Belt
Black Belt

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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Feb 11, 2005 1:33 pm 
Offline
Black Belt 2nd Degree
Black Belt 2nd Degree

Joined: Mon Aug 09, 2004 7:33 pm
Posts: 2740
Location: Milwaukee - Wisconsin
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Feb 11, 2005 2:23 pm 
Offline
Black Belt
Black Belt

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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group