简体   繁体   中英

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

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.

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

You should try:

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

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

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()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM