0

How can enter a whole equation, like 1 + 2. Because I can only so far ask the user to enter one digit at a time, I would like to know how I let the user enter the entire equation?

import java.util.Scanner;
public class TryCalculator {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Addition");
        System.out.println("Subtraction");
        System.out.println("Division");
        System.out.println("Multiplication");
        System.out.println("Natural Log");
        System.out.println("Exponent");
        System.out.println("**********************************");

       Scanner scan = new Scanner(System.in);
       Scanner keyboard = new Scanner(System.in);

       double value1;
       double value2;
       String op;

       System.out.println("enter a digit");
       value1 = scan.nextDouble();
       System.out.println("enter operation ");
       op = keyboard.next();
       System.out.println("enter second digit");
       value2 = scan.nextDouble();

        if(op.equals("+")){
            System.out.println(value1 + value2);

        }if(op.equals("-")){
            System.out.println(value1 - value2);

        }if(op.equals("/")){
            System.out.println(value1 / value2);

        }if(op.equals("*")){
            System.out.println(value1 * value2);

        }if(op.equals("^")){
            System.out.println(Math.pow(value1, value2));

        }if (op.equals("log")){
            System.out.println(Math.log(value1));

        }else{

        }  
    }   
}
adonis
  • 5
  • 2

1 Answers1

-1

If you don't want to use javascript or some evaluators like those ( link1, link2) you can do like this with simple expression like 1 + 2:

Scanner keyboard = new Scanner(System.in);
String exp = keyboard.next();
    String []all = exp.split(" ", -1);
/*
then
all[0] = first operation;
all[1] = operator;
all[0] = second operator
*/

//and you can do like this
void calculate(String []all) {

    Double value1 = Double.parseDouble(all[0]);
    Double value2 = Double.parseDouble(all[2]);
    String op = all[1];

     if(op.equals("+")){
            System.out.println(value1 + value2);

    }if(op.equals("-")){
        System.out.println(value1 - value2);

    }if(op.equals("/")){
        System.out.println(value1 / value2);

    }if(op.equals("*")){
        System.out.println(value1 * value2);

    }if(op.equals("^")){
        System.out.println(Math.pow(value1, value2));

    }if (op.equals("log")){
        System.out.println(Math.log(value1));

    }else{

    }  
}

This is not tested, but can be a good starting point.

Community
  • 1
  • 1
V. Sambor
  • 12,361
  • 6
  • 46
  • 65
  • 1
    *"This is not tested"* Obivously. So test it and fix the bug(s) :). – Tom Apr 24 '16 at 00:34
  • an idea is more than enough in some sitations. I don't have to code all his application. – V. Sambor Apr 24 '16 at 00:37
  • *"I don't have to code all his application."* Who said you should? – Tom Apr 24 '16 at 00:40
  • come on man, why are you messing with me? I just tried to help. If you have a better answer or much time to write a "without bugs" solution...then go ahead, no one stops you to do so... – V. Sambor Apr 24 '16 at 00:52
  • If you're not able to write code which doesn't have obvious bugs, then ok. Keep this answer as it is. And I'm not messing with you. I just said your answer is bugged (with a bit of hope you care about _good_ answers). So no worries, I'm not going to bother you more, here. – Tom Apr 24 '16 at 01:07
  • im new i coding, however i tried your code, but it does not do the calculation . it only let me put the equation e.g. 1 +2 but it doesnt solve it. – adonis Apr 24 '16 at 03:29
  • @adonis if the answer doesn't work, then don't accept it as valid. – Tom Apr 24 '16 at 10:34