1

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.

9
  • 2
    You'll need to write an interpreter. To interpret it. Java doesn't have eval. Commented Dec 10, 2014 at 5:11
  • What exactly does eval do? I'm not sure on the idea so not sure what to write. Commented Dec 10, 2014 at 5:14
  • 1
    Basically, don't try to do stuff like this, unless you're writing a program that actually generates Java source on purpose. You will do better if you design some sort of data structure that holds, for example, list indexes and the strings you want to compare the list elements to. Commented Dec 10, 2014 at 5:15
  • 1
    eval is a function some languages have that allows you to execute code as a String. Java is not one of them. Commented Dec 10, 2014 at 5:16
  • @DimitrisKarittevlis In some interpreted languages like Perl, eval will 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.) Commented Dec 10, 2014 at 5:17

1 Answer 1

2

You can't do this in Java, as it is not an interpreted language. Instead, redesign your code. Something like this might get you on the right track:

public static boolean dynamicIf(String a, String b, String c) {

    final String[] list = {"one", "two", ""};

    boolean value = true;

    if (!a.isEmpty()) {
        value &= a.equals(list[0]);
    }

    if(!b.isEmpty()) {
        value &= b.equals(list[1]);
    }

    if (!c.isEmpty()) {
        value &= c.equals(list[2]);
    }

    return value;
}

This should give you the same results you're looking for. Example:

if (dynamicIf("one", "two", ""))      // true
if (dynamicIf("", "two", ""))         // true
if (dynamicIf("", "", ""))            // true
if (dynamicIf("one", "two", "three")) // false

Edit: This may or may not be what you're looking for. You've left a comment that added more context to the problem and makes me think otherwise, but your desired end result is still unclear.

Sign up to request clarification or add additional context in comments.

1 Comment

Tested this out and seems to be working well. Will do a few more tests tomorrow and then choose this as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.