0

I am clueless about how to change this into arithmetic expressions.

It is supposed to do the same thing but instead of using 3 inputs from the user it is supposed to be 1 input.

The readDouble(); and readChar(); are premade scanner methods that are imported from a package. Help is needed.

import static dit948.SimpleIO.*;

public class Exce1 {
public static void main(String[] args) {
    double n1, n2;
    char operation;



    println("Enter the first number: ");
    n1 = readDouble();

    if(n1 == 0){
        println("The program is terminated. Bye");
        System.exit(0);
    }


    println("Enter the operator: ");
    operation = readChar();

    println("Enter the second number: ");
    n2 = readDouble();

    switch(operation){
    case '+':
    println("The result is " + (n1 + n2));
    break;
    case '-':
        println("The result is " + (n1 - n2));
        break;
    case '*': 
        println("The result is " + (n1 * n2));
        break;
    case '/':
        println("The result is " + (n1 / n2));
        break;
    }
        }
}
Wtower
  • 18,848
  • 11
  • 103
  • 80
Aksel
  • 1

2 Answers2

1

Use ScriptEngineManager & ScriptEngine to perform evaluation of expression

    import java.util.Scanner;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptEngine;
    import javax.script.ScriptException;

    public class myCalc {
        public static void main(String args[]){


                Scanner input = new Scanner(System.in);

                String x="";
                boolean done = false;


                    while (done==false){
                    System.out.print("Please enter your operation: ");

                    x = input.nextLine();


                    ScriptEngineManager mgr = new ScriptEngineManager();
                    ScriptEngine engine = mgr.getEngineByName("JavaScript");

                    try {
                        System.out.println(engine.eval(x));
                    } catch (ScriptException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    }

                       input.close();

        }
    }

Also, someone commented that OP is a learner (it doesn't matter though as it never hurts to try optimal solutions), here's another solution -

import java.util.Scanner;


public class apples {
    public static void main(String args[]){


            Scanner input = new Scanner(System.in);
            double answer = 0;
            double inputA, inputB;
            char operator;
            String x="";
            String[] oper;
            String[] nos;
            boolean done = false;


                while (done==false){
                System.out.print("Please enter your operation: ");

                x = input.nextLine();
                oper = x.split("[^*/+-]");
                nos = x.split("[^0-9]");


                inputA = Double.parseDouble(nos[0]);
                inputB = Double.parseDouble(nos[nos.length-1]);
                operator = oper[oper.length-1].charAt(0);



               switch (operator) {
                    case '+': answer = inputA + inputB;
                              break;
                    case '-': answer = inputA - inputB;
                              break;
                    case '*': answer = inputA * inputB;
                              break;
                    case '/': answer = inputA / inputB;
                              break;

                }

                   System.out.println(answer);   
                }
                   input.close();

    }
}
Techidiot
  • 1,921
  • 1
  • 15
  • 28
0

Because it seems like you are just a learner here is some example code you must go through:

  public static void main(String[] args) {
    // TODO code application logic here
    String exp=null;
    Scanner scan=new Scanner(System.in);

    System.out.println("Please enter an Arithmetic Expression! (2*2)");
    exp=scan.nextLine(); //read the whole line

    //check if the string contains +, -, / or * and then split and perform the required function
    if (exp.contains("+")) {
        String[] array=exp.split("\\+");
        int n1=Integer.parseInt(array[0]);
        int n2=Integer.parseInt(array[1]);
        int sum=n1+n2;
        System.out.println("Answer: "+sum);
    }

    if (exp.contains("-")) {
        String[] array=exp.split("\\-");
        int n1=Integer.parseInt(array[0]);
        int n2=Integer.parseInt(array[1]);
        int sub=n1-n2;
        System.out.println("Answer: "+sub);
    }

    if (exp.contains("/")) {
        String[] array=exp.split("\\/");
        int n1=Integer.parseInt(array[0]);
        int n2=Integer.parseInt(array[1]);
        int div=n1/n2;
        System.out.println("Answer: "+div);
    }

    if (exp.contains("*")) {
        String[] array=exp.split("\\*");
        int n1=Integer.parseInt(array[0]);
        int n2=Integer.parseInt(array[1]);
        int mul=n1*n2;
        System.out.println("Answer: "+mul);
    }
}
Java Nerd
  • 958
  • 3
  • 19
  • 51