Wednesday, September 30, 2009

Math Expression evaluator



import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ScriptDemo {

public static void main(String[] args) {

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");

try {
String expression = "3+4";
Object result = engine.eval(expression);
System.out.println(expression+" = "+result);
} catch(ScriptException se) {
se.printStackTrace();
}
}
}

No comments:

Post a Comment