-4

There are lot of math expression evaluators out there, but they evaluate something like (1 + 2)/3, and so on. But I need to evaluate expression which contains variables (in java). For example, if the program has two variables int a = 1; int b = 2; and if I pass something like (a + b)/a to a method as a String, it should return the result as 3 which is actually (1+2)/1. Actually the formula/expression comes from a config file.

Edit: Just found a similar question Evaluating math expression on dictionary variables , but need the solution in java

user3366706
  • 1,529
  • 3
  • 31
  • 54

2 Answers2

0

As far as I know, there is no way to translate a String into a variable name in Java. What you might want to do instead, is have a mapping from Strings to ints. Then you can parse the command and retrieve the values associated with those Strings.

For example:

HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);

The difference is that now instead of storing the values in a variable, you associate them with a String which you can find in your input text.

For example:

Scanner scan = new Scanner("a b c");
String str1 = scan.next();
int val1 = map.get(str1);

Now val has the value 1 stored in it, which you associated with the String "a" earlier.

The next step is writing a parser for the arithmetic. That's a whole different animal to deal with. If you're looking for something quick to implement, I recommend trying to use Reverse Polish Notation if that's possible. In any case, I don't believe that there's a quick solution that will do all of this including the parsing automatically.

You might want to take a look at this link here: Evaluating a math expression given in string form. It's not quite what you're trying to do, but could be helpful.

Community
  • 1
  • 1
Akshay
  • 814
  • 6
  • 19
0

There are a few parsers which will let you evaluate a string expression, but you must map the variables in some manner. I rolled my own for numeric expressions, which I'd use for your example as follows:

int a = 1;                            //Given some variables
int b = 2;

Evald ex = new Evald();               //and an expression
ex.parse("(a + b) / a");

ex.addVariable("a", a);               //map the variables
int bIndex = ex.addVariable("b", b);  //optionally saving an index mapping

double dResult = ex.evaluate();       //evaluate (sets dResult to 3.0)

a = 3;                                //update your variables 
b = 9;
ex.addVariable("a", a);               //map new values using the string
ex.setVariable(bIndex, b);            //or saved index

int iResult = (int)ex.evaluate();     //evaluate (sets iResult to 4)

I've used more versatile solutions (ranging from Jep which uses a similar syntax to handle strings, vectors, complex numbers etc, to scripting languages such as Lua), and the choice usually comes down to the software requirements - versatility, licensing, library dependencies and execution speed.

BenM
  • 86
  • 3