Java Language Nashorn JavaScript engine Evaluate Arithmetic Strings


// Obtain an instance of JavaScript engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");

//String to be evaluated
String str = "3+2*4+5";
//Value after doing Arithmetic operation with operator precedence will be 16

//Printing the value
try {
} catch (ScriptException ex) {

//Value of the string after arithmetic evaluation is printed on standard output.
//In this case '16.0' will be printed on standard output.