【问题标题】:The method valueOf(String) in the type Integer is not applicable for the arguments (int)Integer 类型中的方法 valueOf(String) 不适用于参数 (int)
【发布时间】:2015-03-09 21:00:39
【问题描述】:

我收到一个错误当我在 jdk1.4(j2sdk1. 4.1_05)。如果我在 jdk1.6.0_14 中编译相同的代码,代码可以正常工作。

HashMap offMap = new HashMap(); offMap.put("price", Integer.valueOf(offer.getPrice()));

我的问题是为什么这段代码在 jdk1.4 而不是 jdk1.6 中编译时会出错。有什么建议可以编译吗?

注意:这是预先编写的代码。需要您对代码更改的建议,并在没有错误的情况下编译它。

【问题讨论】:

    标签: java jdk1.6 value-of jdk1.4


    【解决方案1】:
    【解决方案2】:

    对于 Java 1.4,我认为您需要 Integer(int) 构造函数

    offMap.put("price", new Integer(offer.getPrice()));
    

    Integer.valueOf(int) 在 Java 1.4 中不可用(Javadoc 说它是在 Java 1.5 中添加的)。

    【讨论】:

      【解决方案3】:

      因为在更高版本的 Java 中,Integer 类有一个方法valueOf(int)

      你应该试试:

      offMap.put("price", new Integer(offer.getPrice()));
      

      【讨论】:

        【解决方案4】:

        无论“offer.getPrice()”中的值是什么,它都不应该在字符串中,如果是这样,它会像你上面提到的那样显示错误

        所以你应该试试下面的代码

        offMap.put("price", Integer.parseInt(offer.getPrice().toString()));

        offMap.put("price", Integer.valueOf(offer.getPrice().toString()));

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-31
          • 2018-12-20
          • 1970-01-01
          • 1970-01-01
          • 2013-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多