将字符串评估为条件 Java [英] Evaluate String as a condition Java
本文介绍了将字符串评估为条件 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须从 D/B 中检索一组列值并将其作为条件进行检查.
I have to retrieve a set of column values from D/B and check it as a condition.
例如,我将在 D/B 列中包含诸如 "value > 2"
, "4
For example, I will have strings like "value > 2"
, "4 < value < 6"
in a D/B column. (value is the one which is compared all the time). I will have a variable value declared in my code and I should evaluate this condition.
int value = getValue();
if (value > 2) //(the string retrieved from the D/B)
doSomething();
我该怎么做??任何帮助都非常感谢.谢谢.
How can I do this?? Any help is muceh appreciated. Thanks.
推荐答案
以下是使用标准 (Java 1.6+) 脚本库的示例:
Here is an example using the standard (Java 1.6+) scripting library:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Test {
public static void main(String[] args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval("value = 10");
Boolean greaterThan5 = (Boolean) engine.eval("value > 5");
Boolean lessThan5 = (Boolean) engine.eval("value < 5");
System.out.println("10 > 5? " + greaterThan5); // true
System.out.println("10 < 5? " + lessThan5); // false
}
}
这篇关于将字符串评估为条件 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文