Dynamic if statement evaluation problem with string comparison

Posted by Mani on Stack Overflow See other posts from Stack Overflow or by Mani
Published on 2010-05-15T03:22:41Z Indexed on 2010/05/15 4:04 UTC
Read the original article Hit count: 522

Filed under:
|
|
|
|

I tried the example given in this thread to create if statement dynamically using BeanShell. But it is not working fine. Instead of using "age" variable as integer, i have used string in the below example. I am getting "fail" as answer instead of "success".

Can anyone help me?

/*
To change this template, choose Tools | Templates
and open the template in the editor.
*/

import java.lang.reflect.*;
import bsh.Interpreter;

public class Main {
  public static String d;

  public static void main(String args[])
  {
    try {
      String age = "30";

      String cond = "age==30";


      Interpreter i = new Interpreter();

      i.set("age", age);

      System.out.println(" sss" + i.get("age"));

      if((Boolean)i.eval(cond)) {
        System.out.println("success");
      } else {
        System.out.println("fail");
      }
    }
    catch (Throwable e) {
      System.err.println(e);
    }
  }
}

Thanks, Mani

© Stack Overflow or respective owner

Related posts about bean

Related posts about shell