Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Using created string as loop parameter?

In short, the user will input a number (say 1 through 3). This will decide which range of numbers the loop should search through.

switch(input){
case 1:
    searchTerm = "i<10 && i>5";
case 2:
    searchTerm = "i>=10 && i<19";
case 3:
    searchTerm = "i>19 && i<24";
}
while(searchTerm){
    //some function
}


Is this possible? I I've not been able to find a way to use a string as search parameters.

EDIT: I don't think I did a very good job of explaining why I needed this. What is one to do if there are different numbers of parameters? For example:

case 1:
    searchTerm = "i<5"
case 2:
    searchTerm = "i>25 && i<29"
case 3:
    searchTerm = "(i<50 && i>25) && (i>55 && i<75)"
case 4:
    searchTerm = "(i<20 && i>15) && (i>300 && i<325) && (i>360 && i<380)

Then how does one do it? Multiple loops that call the same function?

like image 314
Jimpy Avatar asked Aug 11 '26 11:08

Jimpy


2 Answers

The correct way to do this is to not use a string at all:

int min, max;

switch(input){
case 1:      // i<10 && i>5
    min = 6;
    max = 10;
    break; // to avoid follow-through to the next case
case 2:     // i>=10 && i<19
    min = 10;
    max = 20;
    break;
case 3:     // i>19 && i<24
    min = 20;
    max = 25;
    break;
default:
    // You need something here in case the value entered wasn't 1-3
}
for (int i = min; i < max; ++i) {
    // ...
}

Re your edit:

I don't think I did a very good job of explaining why I needed this. What is one to do if there are different numbers of parameters?

In that case, you'll have to use an expression evaluator (or write one, which is a non-trivial task). There's one in Spring, for instance (not recommending, just happened to hear about it). A search for "Java expression evaluator" should turn up some options.

Another alternative, which is somewhat amusing given that some folks mistook your question for a JavaScript question, is to use the JavaScript evaluator built into Java (either Rhino or Nashorn). E.g.: Live Example

import javax.script.*;

class Ideone {
    public static void main(String[] args) throws java.lang.Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        String searchTerm = "i >= 19 && i <= 24";
        int i;
        try {
            i = 19;
            engine.put("i", i);
            while ((boolean)engine.eval(searchTerm)) {
                System.out.println("i = " + i);
                ++i;
                engine.put("i", i);
            }
            System.out.println("Done");
        } catch (ScriptException scriptException) {
            System.out.println("Failed with script error");
        }
    }
}

...but you'll still have the problem of determining what initial value to use for i, which I've hardcoded above.

like image 97
T.J. Crowder Avatar answered Aug 13 '26 01:08

T.J. Crowder


In Java 8 you can select a lambda instead of String:

Predicate<Integer> searchTerm = (Integer v) -> false;

switch (input) {
  case 1:
    searchTerm = (Integer v) -> v < 10 && v > 5;

    break;
  case 2:
    searchTerm = (Integer v) -> v >= 10 && v < 19;

    break;
  case 3:
    searchTerm = (Integer v) -> v > 19 && v < 24;

    break;
}

while (searchTerm.test(i)) {
  ...
}
like image 25
Dmitry Bychenko Avatar answered Aug 13 '26 02:08

Dmitry Bychenko