My code looks like this:
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
int x = 10;
engine.eval("x =" + x);
System.out.println((Boolean) engine.eval("x < 5"));
System.out.println((Boolean) engine.eval("2 < x < 5"));
The first SOP prints false as expected, but the second SOP prints true. It doesn’t give correct results when I compare the variable with two values. It gives true, even if half of the condition is true. Any workaround for this? Please suggest. Thanks.
2 < x < 5doesn’t do what you think it does. It’s evaluated as follows:Try
(2 < x) && (x < 5)instead.