43

Is there a way in Java to get the result from this mathematical expression:

String code = "5+4*(7-15)";

In other hand what's the best way to parse an arithmetic expression?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287

10 Answers10

36

You can pass it to a BeanShell bsh.Interpreter, something like this:

Interpreter interpreter = new Interpreter();
interpreter.eval("result = 5+4*(7-15)");
System.out.println(interpreter.get("result"));

You'll want to ensure the string you evaluate is from a trusted source and the usual precautions but otherwise it'll work straight off.

If you want to go a more complicated (but safer) approach you could use ANTLR (that I suspect has a math grammar as a starting point) and actually compile/interpret the statement yourself.

Nick Holt
  • 33,455
  • 4
  • 52
  • 58
  • 8
    Good idea, but problematic with untrusted input, as it would allow "script injection" (see my other comment). – sleske Sep 16 '09 at 11:02
  • @sleske: yeah, you'd certainly have to be careful with injection attacks if the string is user entered. – Nick Holt Sep 16 '09 at 11:07
  • 1
    Can also use built-in JavaScript (Rhino) interpreter now. Although seems more complex to get an eval. And still have a security risk... – PhiLho Sep 16 '09 at 12:05
  • 1
    @PhiLho: BeanShell was just something I used a few years ago, but I agree that something based on the `javax.script` API (I think Rhino is) would be better. – Nick Holt Sep 16 '09 at 12:45
  • +1 for beanshell, being part of the JRE – Thorbjørn Ravn Andersen Sep 16 '09 at 13:28
  • What would this return if the expression were "2/3"? Would it return 0 like Java does? You could stick a typecast in front, but then what about "(2/3)+(1/2)"? – dreeves May 06 '10 at 04:38
  • Trying to use beanshell... How can i guarantee that the result will always be in DOUBLE type? – marcolopes Apr 22 '12 at 18:36
31

i recently developed a expression parser and released it under the apache license. you can grab it at http://projects.congrace.de/exp4j/index.html

hope that helped

fasseg
  • 17,504
  • 8
  • 62
  • 73
  • 2
    Beautiful, incredibly simple to use and works great for my needs. – Kevin Cooper Oct 12 '12 at 20:52
  • 1
    I won't be using this, because I actually need to parse one single expression throughout my application (from a configuration file), but if I had more and couldn't workaround the need, I'd certainly use this! I just had a look at this and it looks amazing (even custom functions are supported!). Great work! – Igor Apr 15 '14 at 14:52
9

You can use the ScriptEngine class and evaluate it as a javascript string

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");        
Object result = engine.eval("5+4*(7-15)");

Indeed , yu should know that the result of the following instruction in javascript :

   eval('var aa=5+4*(7-15)')
   aa // -27

There may be a better way, but this one works.

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
6

Recently I was using very mature math expression parser library, open source, giving the same API for JAVA and .NET. The library name is mXparser. mXparser provides basic functionalities (simple formulas parsing and calculation) and more advanced ones (i.e. user defined arguments, functions). Additionally it is worth to notice that mXparser has rich built-in math collection (meaning operators, unary / binary / variadic functions, iterated operators such as summation and product).

https://mathparser.org/

https://mathparser.org/mxparser-tutorial/

Please find below a few examples to have more clear view on the syntax.

Example 1 - simple formula

Expression e = new Expression("2+3");
double v = e.calculate();

Example 2 - built-in function

Expression e = new Expression("2+sin(3)");
double v = e.calculate();

Example 3 - built-in constants

Expression e = new Expression("2+sin(pi)");
double v = e.calculate();

Example 4 - user defined arguments and constants

Argument x = new Argument("x = 5");
Constant a = new Constant("a = 2 + sin(3)");
Expression e = new Expression("a + x^2", x, a);
double v1 = e.calculate();
x.setArgumentValue(10);
double v2 = e.calculate();

Example 5 - user defined functions

Function f = new Function("f(x,y) = x^2 + cos(y)");
Expression e = new Expression("f(10,pi) - 3", f);
double v = e.calculate();

Example 6 - user defined recursion

Function factorial = new Function("fact(n) = if( n > 0; n*fact(n-1); 1)");
Expression e = new Expression("fact(10) - 10!", factorial);
double v = e.calculate();

Found recntly - in case you would like to try the syntax (and see the advanced use case) you can download the Scalar Calculator app that is powered by mXparser.

Best regards

LK

Leroy Kegan
  • 1,156
  • 10
  • 9
5

Probably not in as straight forward a manner as you are hoping!

But perhaps you could use a javax.script.ScriptEngine and treat the string as a ECMAScript expression, for example?

Take a look at: Scripting for the Java Platform.

Andy
  • 8,870
  • 1
  • 31
  • 39
  • 5
    That is rather dangerous, as it would allow "script injection" (similar to SQL injection). Proceed with caution. – sleske Sep 16 '09 at 10:59
  • Good point. I suppose it depends on what the source of the expressions is. – Andy Sep 16 '09 at 11:01
  • A regex could be used to strip all "non-math" characters from the input string. Would that secure the application from script injection? (Not planning on using this, but just came across it and it made me curious) – FThompson Dec 01 '12 at 17:55
4

There is no builtin way of doing that. But you can use one of the many many open source calculators available.

soulmerge
  • 73,842
  • 19
  • 118
  • 155
1

There is no direct support in the Java SDK for doing this.

You will either have to implement it yourself (possibly using a parser generator such as JavaCC), or use an existing library.

One option would be JEP (commercial), another JEval (free software).

sleske
  • 81,358
  • 34
  • 189
  • 227
0

There's an open-source tool called formula4j that does that job.

To take your example expression, it would be evaluated like this using formula4j:

Formula formula = new Formula("5+4*(7-15)");

Decimal answer = formula.getAnswer(); //-27

John
  • 1,635
  • 15
  • 22
0

You coul use that project

How to use:

double result = 0;
String code = "5+4*(7-15)";
try {
    Expr expr = Parser.parse(code);
    result = expr.value();
} catch (SyntaxException e) {
    e.printStackTrace();
}
System.out.println(String.format("Result: %.04f", result));
Jc Miñarro
  • 1,391
  • 10
  • 18
-2
public static int calc(String string){
    int result=0; 
    String numbers="0123456789";
    String operations="+-/*";
    for (int i=0;i<string.length();i++){
        if (numbers.contains(string.charAt(i)+"")){
            result=result*10+(Integer.parseInt(string.charAt(i)+""));
            }
        else {
            if (string.charAt(i)=='+'){ result+=calc(string.substring(i+1));}
            if (string.charAt(i)=='-'){ result-=calc(string.substring(i+1));}
            if (string.charAt(i)=='*'){ result*=calc(string.substring(i+1));}
            if (string.charAt(i)=='/'){ try{result/=calc(string.substring(i+1));}
                catch (ArithmeticException e){
                    System.err.println("You cannot devide by Zero!");}
            }  
            break;
        }        
    }
    return result;
}