1

Is there any simple library in java which can parse a string as input and produce result after evaluating the input string. Such as- I will give a String like 5+5*(4/2)-7+1 and any of the library method will return me the output: 9.

stealthjong
  • 10,858
  • 13
  • 45
  • 84
Mukla.C
  • 53
  • 3
  • 9

1 Answers1

2

Java provides a built-in JavaScript syntax processing:

javax.script.ScriptEngineManager mgr = new javax.script.ScriptEngineManager();
javax.script.ScriptEngine engine = mgr.getEngineByName("JavaScript");
String res = "5+5*(4/2)-7+1";
System.out.println(engine.eval(res));
Akhilesh Dhar Dubey
  • 2,152
  • 2
  • 25
  • 39