-1

I want to make a calculator in java.

I need to know that is there any way to solve a String Mathematically?

for example:

String str = "3+2*6";

and somehow I'll get:

int result = 15;

1 Answers1

1

Try this:

ScriptEngineManager mg = new ScriptEngineManager();
ScriptEngine engine = mg.getEngineByName("JavaScript");
String val = "3+2*5";
System.out.println(engine.eval(val));
Maroun
  • 94,125
  • 30
  • 188
  • 241
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115