I think you should use expression libraries.May be this post will help you Built-in method for evaluating math expressions in Java
For Evaluating Logical Expressions refer following library : JANINO
EXAMPLE
import java.lang.reflect.InvocationTargetException;
import org.codehaus.commons.compiler.CompileException;
import org.codehaus.janino.ExpressionEvaluator;
public class WorkSheet_1{
public static void main(String[] args) throws CompileException, InvocationTargetException {
ExpressionEvaluator ee = new ExpressionEvaluator(
"a != 0 && b > 5",
boolean.class,
new String[] { "a", "b" },
new Class[] { int.class, int.class }
);
Boolean res1 = (Boolean) ee.evaluate(new Object[] {new Integer(2), new Integer(6),});
System.out.println("res1 = " + res1);
Boolean res2 = (Boolean) ee.evaluate(new Object[] {new Integer(2), new Integer(5),});
System.out.println("res2 = " + res2);
}
}
OUTPUT
res1 = true
res2 = false