Sign up for Free !!
World's first open source developer community with
Ticketing System
Tried writing a Java routine to evaluate math expressions from String values like:
Don’t want a lot of if-then-else statements. How to write it?
With JDK1.6, you can use the built-in Javascript engine.
import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; import javax.script.ScriptException; public class Test { public static void main(String[] args) throws ScriptException { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); String foo = "40+2"; System.out.println(engine.eval(foo)); } }
Please Login/Register to write your answer !!!
With JDK1.6, you can use the built-in Javascript engine.