Reputation: 984
I have a string containing a mathematical operation (programmatically generated), something like
(1000 + 700) * (50 + 25) = (10000 - 5000) // it's an example
I need a method that I have to pass this string to it, and it will return true if the operation is valid else it will return false. Any ideas on how can I achieve this?
Upvotes: 0
Views: 484
Reputation: 984
I have found the solution to my problem, I am using the library written by @fas http://www.objecthunter.net/exp4j/
It is really a useful library, you can use it like this:
String left = (1000 + 700) * (50 + 25);
String right = (10000 - 5000);
Calculable calc = new ExpressionBuilder(leftCondition).build();
double result1 = calc.calculate();
calc = new ExpressionBuilder(rightCondition).build();
double result2 = calc.calculate();
System.out.println(result1 + " ------------- " + result2);
Upvotes: 0
Reputation: 63
There's a lovely thing called an infix expression parser. You'll have to write your own, but if you look up how to do an infix expression parser that would be the best thing to do.
It basically looks for all things: correct syntax, and you can code it to look for mathematical validation on both sides of the equals sign.
Upvotes: 2