• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting numberformatexception For input string: "500.0"  RSS feed

 
Niklesh Mudiraj
Greenhorn
Posts: 12
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When i try to parse the value of initial_tuition_fee_local to integer it showing me an number format exception.Above is the code snape for reference please help me to resolute it
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"500.0" is not an Integer, it has decimal.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to ignore the decimal part, you may try new Float(initial_tuition_fee_local.toString()).intValue();

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsha Smith wrote:If you want to ignore the decimal part, you may try new Float(initial_tuition_fee_local.toString()).intValue();

Make that Float.valueOf(initial_tuition_fee_local.toString()).intValue();. Since Java 5.0 you shouldn't use new with any of the primitive wrappers anymore. All of them have valueOf methods that will do the same but use an internal cache of objects (if present).
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats correct! always use valueOf instead of new.

You may use overloaded String.valueOf() method instead of .toString() when working with primitives or char[] or wrapper type objects.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alternative solution:

if(initial_tuition_fee_local.toString().contains("."){

initial_tuition_fee_local = Integer.valueOf(String.valueOf(initial_tuition_fee_local).split("[.]")[0])
}
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.split uses regular expressions, and is relatively "heavy" compared to the simpler indexOf + substring solution. Also, don't use Integer.valueOf to get an int - it will possibly create an Integer and then call intValue() on that. Use parseInt. And String.valueOf(initial_tuition_fee_local) is the same as initial_tuition_fee_local (except when it's null).
 
Niklesh Mudiraj
Greenhorn
Posts: 12
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks , but i want to know why to use valueOf() instead of new. or why the valueOf is better option instead of new
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No new object is created on the heap if it already exists(using valueOf) ...that saves memory. one of the good ways to prevent out of memory error.

@Rob Spoor

Good stuff!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!