Say a string contains the expression 1<3... how can I evaluate that expression in an if statement?
import java.io.*;
public class EvaluateExpression
{
public static void main(String[]args)throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
String expression;
System.out.print("Enter the expression to evaluate: ");
expression = in.readLine();
if (expression){
System.out.print("The expression " + expression + " is true.");
}else{
System.out.print("The expression " + expression + " is not true.");
}
}
}
A boolean is required but a String is found. Is there a way to evaluate the string so it returns a boolean?
if (1<3) {...}
works but if (expression) {...}
where the String variable expression is 1<3 doesn't work. Casting it to boolean doesn't work either.
Thanks!