0

I need to create a method in main to read string expression and parse it with parenthesis first.

the expression is:
(3+4)*7/2

It has to be done in java. But i don't know how to do it.

Any ideas on that!

Nitesh Verma
  • 1,795
  • 4
  • 27
  • 46

1 Answers1

2

Try this:

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "(3+4)*7/2";
double result=0;
try {
    /*
    It seems that (on some platforms) this results in a java.lang.RuntimeException 
    because of converting Object to double, so let's replace it with
    Double.doubleValue()
    */
    //result = (double) (engine.eval(foo));
    result = ((Double) (engine.eval(foo))).doubleValue(); //result = 24.5
} catch (ScriptException e) {
    //handle exception here
}

UPDATE

Before you try to evaluate the expression, you should test if the "JavaScript" ScriptEngine is registered on your system, you can do it like this:

ScriptEngineManager manager = new ScriptEngineManager();
List<ScriptEngineFactory> factories = manager.getEngineFactories();
for (ScriptEngineFactory factory : factories) {
    System.out.println(factory.getNames());
}

My output is

[js, rhino, JavaScript, javascript, ECMAScript, ecmascript]

If your output doesn't contain JavaScript, then try with js and javascript, like

ScriptEngine engine = mgr.getEngineByName("js");

or

ScriptEngine engine = mgr.getEngineByName("javascript");

If neither of them exist you can't use js as script engine to evaluate your expression

BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • I tried this code and it did not compile, shouldn't (double) be (Double)? As Double extends Object but double does not – Richard Tingle Apr 25 '13 at 11:16
  • @Richtea Don't know why it doesn't compile, on my pc it does compile without problems. Check if it generates a ScriptException, if it does, then "JavaScript" is not a registered ScriptEngine in your JRE. See my update to know how to list available script engines – BackSlash Apr 25 '13 at 11:25
  • With (double) changed to (Double) it runs without problems on my PC in netbeans. With (double) it creates the following exception: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - inconvertible types required: double found: java.lang.Object at test.ScratchSpace.main(Test.java:22) Java Result: 1 – Richard Tingle Apr 25 '13 at 11:32
  • javascript is indeed available, I got [js, rhino, JavaScript, javascript, ECMAScript, ecmascript] as output – Richard Tingle Apr 25 '13 at 11:35
  • @Richtea then i don't know what's wrong, what java version do you have? – BackSlash Apr 25 '13 at 12:06
  • java version 1.7.0_01. Possibly its netbeans, but isn't this behaviour "as designed", I mean an Object isn't supposed to be castable as a double as double doesn't extend Object – Richard Tingle Apr 25 '13 at 12:20
  • @Richtea I have java 1.7.0_07, is some exception being thrown? if yes, what exception do you get? – BackSlash Apr 25 '13 at 12:29
  • Yes I get an exeption exception Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - inconvertible types required: double found: java.lang.Object at test.Test.main(Test.java:28) Java Result: 1 – Richard Tingle Apr 25 '13 at 13:09
  • Thats the line result = (double) (engine.eval(foo)); Netbeans also marks that line in the source code with "inconvertable types" – Richard Tingle Apr 25 '13 at 13:10
  • For me it's working. See it: http://alessandroderoma.it/nb1.png And try to compile the code here: http://www.compileonline.com/compile_java_online.php – BackSlash Apr 25 '13 at 13:21
  • See: http://postimg.org/image/5dvre7ev9/ . Whole thing is a bit bizarre. Still, (Double) presumably works for both of us. Perhaps its a very recent autoUnBoxing feature that allows Object -->Double --> double just by casting as (double). Either way (Double) works for me and I don't want you to worry excessively about it – Richard Tingle Apr 25 '13 at 13:43
  • @Richtea BTW, i edited my answer with the example that should work with every java version – BackSlash Apr 25 '13 at 13:49
  • Awesome, you already had my +1 but if you didn't you would now – Richard Tingle Apr 25 '13 at 13:55