netbeans - method in main read string expression in java -
this question has answer here:
i need create method in main read string expression , parse parenthesis first.
the expression is:
     (3+4)*7/2
it has done in java. don't know how it.
any ideas on that!
try this:
scriptenginemanager mgr = new scriptenginemanager(); scriptengine engine = mgr.getenginebyname("javascript"); string foo = "(3+4)*7/2"; double result=0; try {     /*     seems (on platforms) results in java.lang.runtimeexception      because of converting object double, let's replace     double.doublevalue()     */     //result = (double) (engine.eval(foo));     result = ((double) (engine.eval(foo))).doublevalue(); //result = 24.5 } catch (scriptexception e) {     //handle exception here }   update
before try evaluate expression, should test if "javascript" scriptengine registered on system, can this:
scriptenginemanager manager = new scriptenginemanager(); list<scriptenginefactory> factories = manager.getenginefactories(); (scriptenginefactory factory : factories) {     system.out.println(factory.getnames()); }   my output is
[js, rhino, javascript, javascript, ecmascript, ecmascript]   if output doesn't contain javascript, try js , javascript, like
scriptengine engine = mgr.getenginebyname("js");   or
scriptengine engine = mgr.getenginebyname("javascript");   if neither of them exist can't use js script engine evaluate expression