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,502
  • 13
  • 43
  • 83
Mukla.C
  • 53
  • 2
  • 9
  • 3
    [duplicate](http://stackoverflow.com/q/15173681/1113392) . see [this answer](http://stackoverflow.com/a/15174060/1113392) – A4L Jul 03 '14 at 10:25
  • 2
    possible duplicate of [Evaluating a math expression given in string form](http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form) – Akhilesh Dhar Dubey Jul 03 '14 at 10:25
  • Please try to perform at-least one google search before asking. – Atish Dipongkor Jul 07 '14 at 05:53

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,132
  • 2
  • 25
  • 39