Java字符串和数学表达式求值程序 [英] Java String and Mathematical Expression Evaluators
问题描述
我们使用Jeks解析器来评估表达式。我看不到用它来评估字符串表达式的方法 - 例如:
IF(Test 1=Test 2) / p>
有没有什么可以评估Java中的字符串和数学表达式?感谢任何帮助,
Andez
答案是在Jeks论坛上发贴。我得到了马努的回答。我以为它没有活跃,特别是因为我无法注册 - 但是它已被排序。
但是如果有任何人有同样的问题,那么以下代码行会得到它工作:
ExpressionParser解析器; //我们做了=新的ExpressionParser(new JeksExpressionSyntax(),null);
JeksInterpreter解释器//
并创建解释器:
interpreter = new JeksInterpreter(){
@Override
public Object getBinaryOperatorValue(Object binaryOperatorKey,Object param1,Object param2)
{
//只有函数可以将单元格设置为参数
if(param1 instanceof JeksCellSet || param2 instanceof JeksCellSet)
throw new IllegalArgumentException();
//启用Jeks支持的类型之间的比较
else if(binaryOperatorKey.equals(JeksExpressionSyntax.OPERATOR_EQUAL))
return param1!= null&& param1.equals(param2)
? Boolean.TRUE:Boolean.FALSE;
//启用Jeks支持的类型之间的比较
else if(binaryOperatorKey.equals(JeksExpressionSyntax.OPERATOR_DIFFERENT))
return param1!= null&& param1.equals(param2)? Boolean.FALSE:Boolean.TRUE;
else
return super.getBinaryOperatorValue(binaryOperatorKey,param1,param2);
}
};
parser = new ExpressionParser(new JeksExpressionSyntax(),null);
We use the Jeks parser at present to evaluate expressions. I cannot see a way to evaluate string expressions with it - for example:
IF( "Test 1" = "Test 2")
Is there anything out there that can evaluate string and mathematical expressions in Java? Preferably free or open source.
Thanks for any help,
Andez
The answer was a posting on the Jeks forum. I got an answer from Manu. I thought it was not active especially as I could not register on it - but that got sorted.
But if anyone has the same problem then the following lines of code will get it working:
ExpressionParser parser; // we did have = new ExpressionParser(new JeksExpressionSyntax(), null);
JeksInterpreter interpreter; //
And to create the interpreter:
interpreter = new JeksInterpreter() {
@Override
public Object getBinaryOperatorValue (Object binaryOperatorKey, Object param1, Object param2)
{
// Only functions may take a cell set as parameter
if ( param1 instanceof JeksCellSet || param2 instanceof JeksCellSet)
throw new IllegalArgumentException ();
// Enabled comparison between any type supported by Jeks
else if (binaryOperatorKey.equals (JeksExpressionSyntax.OPERATOR_EQUAL))
return param1 != null && param1.equals (param2)
? Boolean.TRUE : Boolean.FALSE;
// Enabled comparison between any type supported by Jeks
else if (binaryOperatorKey.equals (JeksExpressionSyntax.OPERATOR_DIFFERENT))
return param1 != null && param1.equals (param2) ? Boolean.FALSE : Boolean.TRUE;
else
return super.getBinaryOperatorValue (binaryOperatorKey, param1, param2);
}
};
parser = new ExpressionParser(new JeksExpressionSyntax(), null);
这篇关于Java字符串和数学表达式求值程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!