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.

Safinn
  • 622
  • 3
  • 15
  • 26
  • 2
    You'll need to write an interpreter. To interpret it. Java doesn't have `eval`. – Elliott Frisch Dec 10 '14 at 05:11
  • What exactly does `eval` do? I'm not sure on the idea so not sure what to write. – Safinn Dec 10 '14 at 05: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. – ajb Dec 10 '14 at 05:15
  • 1
    `eval` is a function some languages have that allows you to execute code as a `String`. Java is not one of them. – Elliott Frisch Dec 10 '14 at 05: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.) – ajb Dec 10 '14 at 05:17
  • Hmm.. Seems like I will have to come up with a different way. Basically I'm receiving input from a user which they want to filter for. So I may have the fields `Origin` and `Destination`. If they give me an 'Origin' and 'Destination', I want to filter and return the objects that match the Origin and Destination fields. If they only give me one of them, for example Origin, then i want to return all the objects that match in the Origin field and ignore the Destination field. – Safinn Dec 10 '14 at 05:19
  • @Dimitris If you could provide some code for the objects you are using, that may help. What are they stored in? A list? An array? – Julian Dec 10 '14 at 05:34
  • This looks like an XY problem. Why are you trying to do this? – Raedwald Dec 10 '14 at 07:58
  • See http://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java – Raedwald Dec 10 '14 at 07:59

1 Answers1

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.

Julian
  • 1,688
  • 1
  • 12
  • 19
  • Tested this out and seems to be working well. Will do a few more tests tomorrow and then choose this as the answer. – Safinn Dec 10 '14 at 06:34