0

I don't know if this is possible. I'm in a situation where the input is going to look like this:

[int, String, int]

Where the string is either "<", ">", "=", "<=", ">=".

Is there the way to implement a method that would help me compare the two ints with respect to the string compare parameter without multiple if statements?

I hope I've explained it well.

Thanks.

  • 3
    Check the following link to see if it will help http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form – CM Kanode Nov 05 '12 at 19:27
  • 1
    A similar thread here too http://stackoverflow.com/questions/2605032/using-eval-in-java – scrappedcola Nov 05 '12 at 19:28

1 Answers1

3

There's no way to apply a string as if were an operator in Java source code. You're going to have to test each string value and do the appropriate test.

public boolean eval(int arg1, String op, int arg2) {
    if (op.equals("<")) {
        return arg1 < arg2;
    } else if (op.equals("<=")) {
        return arg1 <= arg2;
    } else . . .
}

There are ways to get very fancy about this (with, for example, a Map<String, Callable<Boolean>>), but it doesn't seem worth it for what you're describing.

Alternatively (as CM Kanode and screppedcola suggest in their comments) you can evaluate an expression as JavaScript, using a ScriptEngine, but this also seems like overkill.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thank you . Is there a reason you would say why the ScriptEngine is overkill? It certainly looks much cleaner. –  Nov 05 '12 at 19:37
  • *"There's no way to apply a string as if were an operator in Java source code."* - Actually, there is. That would be to use the [`JavaCompiler`](http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html) to *evaluate a String of code* (compile and execute new code during runtime). ___ **But be advised**: For pretty obvious reasons, this is extremely inappropriate, and far beyond overkill. – CosmicGiant Nov 05 '12 at 19:55
  • 2
    @isal - Behind the scenes, you would be loading an entirely new language interpreter into Java. It looks cleaner on the surface, but it's like ordering a battalion to kill a fly. Bear in mind, also, (as one of the comments in the post linked to by CM) that you would be running a script, not just evaluating an expression. For instance, pass the string "8;40+2" and you would get back 42. If you want something that looks clean and at least is in scale with your problem, try [Javaluator](http://javaluator.sourceforge.net/en/home/) instead of `ScriptEngine`. – Ted Hopp Nov 05 '12 at 20:32
  • @TheLima - Yeesh! Just...yeesh. I admit I hadn't thought of that. You would have to take the expression, package it into a statement of some kind (presumably as an initializer for some static variable declaration) and package that into a class. Then you would have a compilation unit that the compiler might be able to process. Then, of course, you'd have to load the resulting class file and access the value (using reflection, of course). Far beyond overkill, indeed. – Ted Hopp Nov 05 '12 at 20:38