2010-07-09 176 views
0

我正在爲JEXL做rnd,但我得到以下程序的例外;JEXL程序有「;」異常

 String strDuration = "4560"; 
     long lDuration = Long.parseLong(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); 

例外: 產生的原因:org.apache.commons.jexl2.parser.ParseException:不明確聲明@ 1:30,缺少 ';'表情之間

任何人都可以幫助我顯示我想要的輸出(布爾)嗎?

在此先感謝。

回答

1

在這裏你去:

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); 
    } 

編輯: 要明確的問題是你return語句的使用,它似乎不JEXL支持。

+0

正確並解決 – 2010-07-14 04:22:41