Here is an example of a string expression I am creating that I then want to use in the condition of an if statement.
public static String dynamicIf(String a, String b, String c){
String expression = "";
String[] list = {"one", "two", ""};
if(!a.equals(""))
expression += "a.equals(list[0])";
if(!b.equals(""))
{
if(!expression.equals(""))
expression += " && b.equals(list[1])";
else
expression += "b.equals(list[1])";
}
if(!c.equals(""))
{
if(!expression.equals(""))
expression += " && c.equals(list[2])";
else
expression += "c.equals(list[2])";
}
//String[] splitExpression = expression.split(" && ");
return expression;
}
So the function above creates a string which i would then like to use in the condition of an if statement. The result of the method running with these parameters:
dynamicIf("one","two","");
is
a.equals(list[0]) && b.equals(list[1])
How can I get this expression in a string to run within the condition of an if?
I hope you understand. Thank you.
eval.evaldo? I'm not sure on the idea so not sure what to write.evalis a function some languages have that allows you to execute code as aString. Java is not one of them.evalwill take a string, treat it as Perl code, and execute it. It might be called other things in other interpreted languages. (In APL, there's a weird character to do this.)