English 中文(简体)
说明评价问题与加强比较动态
原标题:Dynamic if statement evaluation problem with string comparison
  • 时间:2010-05-15 03:22:41
  •  标签:
  • java

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".

谁能帮助我?

/*
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

问题回答

你们必须选择数字比较或优势比较。 这需要使用相容的条件和年龄类型。

Numeric:

  int age = 30;
  String cond = "age==30";

努力:

  String age = "30";
  String cond = "age.equals("30")";

当你将两个物体与=操作者进行比较时,你对两个条目进行了比较:参比>。 您基本上询问,两个不同的名称是否指同一物体。

为了比较物体的实际价值,你需要使用<代码>等值()。 这对于了解 Java非常重要。

@Matthew 颜色是正确的。 除此以外,您可以简化以下产出:

System.out.println(cond + " is " + i.eval(cond));

生产

1. 年龄= 30人

你们正在使用<条码>=来比较插图类型。 改用<代码>age. Equals ("30”)。

http://www.ohchr.org。

如果您将这一定义作为<代码>cond的定义:

String cond = "age.equals("30")";

产出:

 sss30
success

针对关于使用<代码>=“30”的问题,此处回答如下:

如果你<代码> 标准年龄:被问到,因为这是一个汇编时常数,那么 ∗即属实。

final String age = "30";

但是,如果你明确<代码>,新的 <代码>String,否则不予干预,即为假。

String age = new String("30");

你们可以举出两个例子,看一看这是否有效。 可能的话,可以同时获得<代码>fail。

现在,仅仅因为相互交织的存在,就意味着应当始终依靠它来比较<条码><> 编码>的类型。 <代码>=的操作者只能相互比较原始数据,比较参比类型,看它们是否与同一目标相提,因此,就我们可以说,看到两个物体是i Equal而不是

有时,通过JVER和JDK的魔法,String和其他原始包装商,如Integer,可与=比较,但这方面的情况有限,而且不可靠。

与“=”译员相比,笔迹并不如预期。

它的工作类似:(从链接灯塔看)

    Beanshell handles  ==  like java and not e.g. like JavaScript. That means you ve got to use "equals" to compare instances.
    bsh % "abc" == "abc"; => true
    bsh % "abc" == new String("abc"); => false
    bsh % "abc".equals("abc"); => true
    bsh % "abc".equals(new String("abc")); => true

这方面的进一步信息:

So you have to use the ".equal()", or compile your own bsh version, like i did it. (read the complete issue above)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...