-3

I'm trying to write a program that will take an input from a user and calculate the total from their input. For example, if the user input "5+30*2" the output would be "65". I've spent hours on this so far and can't seem to get a correct solution. Does anyone have any idea how i would do this?

Philippe Blayo
  • 10,610
  • 14
  • 48
  • 65
user3557212
  • 33
  • 1
  • 5

1 Answers1

0

With JDK1.6, you can use the built-in Javascript engine.

this is tested and working .

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

public class MathExpress {


    public static void main(String[] args) throws ScriptException {
        Scanner console = new Scanner(System.in);

        System.out.println("Enter your arithematic oparation ");
        String Mathex= console.nextLine();

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

        System.out.println(engine.eval(Mathex));




    }
}
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60