-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;
a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843

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
  • 91,013
  • 29
  • 181
  • 233
Ruchira Gayan Ranaweera
  • 33,712
  • 16
  • 72
  • 110