参数类型为Integer的方法valueOf(String)不适用于参数(int)

时间:2015-01-12 06:01:24

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

我收到错误当我在jdk1.4(j2sdk1)中编译代码时,Integer类型中的方法valueOf(String)不适用于下面一行的参数(int)。 4.1_05)。如果我在jdk1.6.0_14中编译相同的代码,代码工作正常。

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

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

注意:这是预先编写的代码。需要你的代码更改建议,并编译没有错误。

4 个答案:

答案 0 :(得分:6)

答案 1 :(得分:2)

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

你应该尝试:

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

答案 2 :(得分:2)

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

offMap.put("price", new Integer(offer.getPrice()));
Java 1.4中没有

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

答案 3 :(得分:1)

无论“ offer.getPrice()”中的值是什么,都不应该在String中,如果是,它将显示错误,如上面提到的

所以您应该尝试以下代码

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

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