开发者

JEXL program have ";" exception

开发者 https://www.devze.com 2023-01-06 01:31 出处:网络
I am doing rnd for JEXL but i got exception for the below program; String strDuration = \"4560\"; long lDuration = Long.pa开发者_C百科rseLong(strDuration);

I am doing rnd for JEXL but i got exception for the below program;

        String strDuration = "4560";
        long lDuration = Long.pa开发者_C百科rseLong(strDuration);
        String theExpression = "" +
                "if(lDuration > 500)" +
                "   return true;" +
                "else" +
                "   return false;";

        Expression e =  jexl.createExpression( theExpression );
        JexlContext context = new MapContext();
        context.set("lDuration", lDuration);
        Boolean result =(Boolean) e.evaluate(context);
        System.out.println("The answer : " + result);

Exception : Caused by: org.apache.commons.jexl2.parser.ParseException: Ambiguous statement @1:30, missing ';' between expressions

Can anyone help me to display the output that i want(the boolean)?

Thanks in advance.


Here you go:

  public static void main(String[] args) {
    String strDuration = "4560";
    long lDuration = Long.parseLong(strDuration);
    String theExpression = "(lDuration > 500) ? true : false;";
    JexlEngine jexl = new JexlEngine();
    Expression e = jexl.createExpression(theExpression);
    JexlContext context = new MapContext();
    context.set("lDuration", lDuration);
    Boolean result = (Boolean) e.evaluate(context);
    System.out.println("The answer : " + result);
  }

Edit: To be clear the problem is your use of the return statement, it appears to not be supported by JEXL.

0

精彩评论

暂无评论...
验证码 换一张
取 消