-5

Introduction

Assuming I have a line of strings "34/2 + 52 * 2 + 3.45" How do I convert and calculate this line of strings into one double value?

Jack
  • 115
  • 1
  • 2
  • 8
  • Use regular expression with Pattern and get the values of operands and operater and then manipulate them – amicngh Aug 02 '13 at 05:52
  • [possible duplicate](http://stackoverflow.com/questions/15173681/string-with-math-operators-to-integer) – A4L Aug 02 '13 at 05:58
  • You can see this link http://stackoverflow.com/questions/2605032/using-eval-in-java – Nitish P Aug 02 '13 at 06:00

4 Answers4

5

One way would be you can use the ScriptEngine class and evaluate it as a javascript string using eval().

You have to use ScriptEngineManager#getEngineByName("js") to load the JS interpreter .

Remember , it is a costly operation.

Better thing would be write a method which evaluates the expression using a Stack. If all the operations are binary operations, it is possible to use a stack to find the overall value of an infix expression by first converting it to postfix notation. Learn Reverse Polish notation.

Lastly , you can look at some of the third-party libraries.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • Are you sure you want to load the entire JS engine just to evaluate a few expressions? – tbodt Aug 02 '13 at 06:08
  • @tbodt I have already mentioned it is a costly expression , but if there are lot of evaluations in his application like the one he posted , what is the harm in doing that ? – AllTooSir Aug 02 '13 at 06:18
  • I'm assuming that is a rhetorical question. – tbodt Aug 02 '13 at 06:22
2

Normally that is done by some kind of lexer/parser which can evaluate this kind of expression using regular expressions(?) and compiler principles. TO be clear you use the lexer to separate your whole expression in tokens, which are the smallest valid unit of your expressions, then use the parser to evaluate the meaning of these tokens as a whole and obtain the result.

Oscar Rene
  • 361
  • 2
  • 6
2

You can use Javascript scripting from Java code:

// create a script engine manager
ScriptEngineManager manager = new ScriptEngineManager();

// create a JavaScript engine
ScriptEngine jsEngine = manager.getEngineByName("JavaScript");
if (jsEngine !=null)
    // evaluate expression from String
    jsEngine.eval("print(34/2 + 52 * 2 + 3.45)");
else
    System.err.println("JavaScript is not available");

OUTPUT:

124.45
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

You have several options (these aren't all of them, just the ones off of the top of my head): 1. Analyze it with regular expressions. This is ideal if you know them. 2. Break it up into substrings, convert those substrings into numbers and symbols, and write a function that turns the entire thing into a proper answer following the order of operations. 3. Call a ScriptEngine and use a language like JavaScript's built in parser functionality to perform the operation. Roundabout, but effective.

Commander Worf
  • 324
  • 1
  • 9