Convert math string to int

Is there an easy way to take a string like "5 * 4" and return 20?

+5
source share
11 answers

I don’t know which ones are better, but there are packages of “evaluators of mathematical expressions”.

check Java Math Expression Evaluator ( single file source code)

An example of use from the site:

java -cp meval.jar com.primalworld.math.MathEvaluator -cos (0) * (1 + 2)
java -cp meval.jar com.primalworld.math.MathEvaluator +0.05 * 200 + 3.01

+3
source

- Rhino JavaScript engine, API JRE 6.

: , , . , , .

+6
+2

googled , . , , , , depsite .

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ScriptDemo {

    public static void main(String[] args) {

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");

        engine.put("a", 1);
        engine.put("b", 2);

        try {
            String expression = "(a + b) > 2";
            Object result = engine.eval(expression);
            System.out.println(expression+" ? "+result);
        } catch(ScriptException se) {
            se.printStackTrace();
        }
    }
}
+2

, JEXL ( Java)

; , :

  public static void main(String[] args) {
    long a = 5;
    long b = 4;
    String theExpression = "a * b";
    JexlEngine jexl = new JexlEngine();
    Expression e = jexl.createExpression(theExpression);
    JexlContext context = new MapContext();
    context.set("a", a);
    context.set("b", b);
    Long result = (Long) e.evaluate(context);
    System.out.println("The answer : " + result);
  }

, :

 public static void main(String[] args) {
   JexlEngine jexl = new JexlEngine();
   Expression e = jexl.createExpression("5 * 4");
   Integer result = (Integer) e.evaluate(null);
   System.out.println("The answer : " + result);
 }
+2
+1

, Integer.parseInt() switch(), .

javax.script.ScriptEngine; . http://forums.sun.com/thread.jspa?threadID=5144807.

+1

BeanShell.
Java, , ( eval BeanShell, - ):

import bsh.EvalError;
import bsh.Interpreter;

public class EVAL {

    private static final String FORMAT = "%-5s | %-5s | %-5s | %s%n";

    public static void main(String[] args) {
        tabela("((a && b)||c)");
        tabela("a ? (b || c) : (b && c)");
        tabela("(a?1:0) + (b?1:0) + (c?1:0) >= 2");
    }

    private static void tabela(String expressao) {
        System.out.printf(FORMAT, "  a  ", "  b  ", "  c  ", expressao);
        System.out.printf(FORMAT, "-----", "-----", "-----", expressao.replaceAll(".", "-"));
        try {
            for (int i = 0; i < 8; i++) {
                boolean a = (i & (1<<2)) != 0;
                boolean b = (i & (1<<1)) != 0;
                boolean c = (i & (1<<0)) != 0;
                boolean r = eval(expressao, a, b, c);
                System.out.printf(FORMAT, a, b, c, r);
            }
        } catch (EvalError ex) {
            ex.printStackTrace();
        }
        System.out.println();
        System.out.println();
    }

    private static boolean eval(String expressao, boolean a, boolean b, boolean c) throws EvalError {
        Interpreter inter = new Interpreter();
        inter.set("a", a);
        inter.set("b", b);
        inter.set("c", c);
        Object resultado = inter.eval(expressao);
        return (Boolean) resultado;
    }
}

:

  a   |   b   |   c   | ((a && b)||c)
----- | ----- | ----- | -------------
false | false | false | false
false | false | true  | true
false | true  | false | false
false | true  | true  | true
true  | false | false | false
true  | false | true  | true
true  | true  | false | true
true  | true  | true  | true


  a   |   b   |   c   | a ? (b || c) : (b && c)
----- | ----- | ----- | -----------------------
false | false | false | false
false | false | true  | false
false | true  | false | false
false | true  | true  | true
true  | false | false | false
true  | false | true  | true
true  | true  | false | true
true  | true  | true  | true


  a   |   b   |   c   | (a?1:0) + (b?1:0) + (c?1:0) >= 2
----- | ----- | ----- | --------------------------------
false | false | false | false
false | false | true  | false
false | true  | false | false
false | true  | true  | true
true  | false | false | false
true  | false | true  | true
true  | true  | false | true
true  | true  | true  | true
+1

I have used JEval in the past and found it quite simple and intuitive. Here is the code snippet:

import net.sourceforge.jeval.EvaluationException;
import net.sourceforge.jeval.Evaluator;

public class Main {
    public static void main(String[] args) {
        try {
            System.out.println(new Evaluator().evaluate("5+4*3"));
        }
        catch (EvaluationException e) {
            e.printStackTrace();
        }
    }
}
+1
source

There is this one:

https://eval.dev.java.net/
0
source

All Articles