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
.
Asked
Active
Viewed 3,162 times
1

stealthjong
- 10,858
- 13
- 45
- 84

Mukla.C
- 53
- 3
- 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
-
2possible 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 Kumar Dipongkor Jul 07 '14 at 05:53
1 Answers
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