private String boundaries = "(x > 750) & (x < 900)";
if (boundaries) {
//Do stuff
}
I wanted to be able to use the string as the variable in the if clause, and then change the string independently without having to edit the code itself. The example idea doesn't work, I was wondering if there is a mistake in it or another way to do this without using strings.
Thank you.
Java is not a dynamically compiled language, so if you want to evaaluate such an expression you can use Javascript from Java.
Try this code:
String boundaries = "(x > 750) & (x < 900)";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
engine.eval("var x = 810;"); // Define x here
engine.eval("var bln = false;")
engine.eval("if (" + boundaries + ") bln = true; ")
if((boolean)engine.eval(bln)) {
// Do stuff
}
Hope this helps. Reference: Oracle: Java Scripting Programmer's Guide
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With