The method valueOf(String) in the type Integer is not applicable for the arguments (int)

6.7k Views Asked by At

I am getting an error The method valueOf(String) in the type Integer is not applicable for the arguments (int) for the below line when I compile the code in jdk1.4(j2sdk1.4.1_05). If I compile the same code in jdk1.6.0_14, the code works fine.

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

My question is why this code is giving error when compiled in jdk1.4 and not in jdk1.6. Any suggestions to get in compiled?

Note: This is pre-written code. Need your suggestion for code change and get it compiled without errors.

4

There are 4 best solutions below

0
On BEST ANSWER

For Java 1.4, I think you want the Integer(int) constructor like

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

Integer.valueOf(int) wasn't available in Java 1.4 (the Javadoc says it was added in Java 1.5).

0
On

whatever value is in "offer.getPrice()", it shouldn't be in String and if it is so,it will show Error like you mentioned above

so you should try the below code

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

or

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

0
On
0
On

Because in later versions of Java, the Integer class has a method valueOf(int).

You should try:

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