• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormatException

 
Shafian Kisna
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

This is very urgent....I am unable to resolve the NumberFormatException error...
The code is:-
String temp = Double.toString(rs.getDouble("AMOUNT"));
int halfDay = (Integer.parseInt(temp.trim())) / 2;

String halfDayStr = new Integer(halfDay).toString();

BTW, AMOUNT in the db is of Decimal type. 12byte.

Appreciate your assistance.

Best Regards
SK
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shafian Kisna:
Hi Everyone,

This is very urgent....I am unable to resolve the NumberFormatException error...
The code is:-
String temp = Double.toString(rs.getDouble("AMOUNT"));
int halfDay = (Integer.parseInt(temp.trim())) / 2;

String halfDayStr = new Integer(halfDay).toString();

BTW, AMOUNT in the db is of Decimal type. 12byte.

Appreciate your assistance.

Best Regards
SK




Why dont you check the value of your temp variable, before supplying it to Integer.parseInt().

This will give the answer of your problem.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think trim() does ?
If you understand what trim() does, you'll unserstand why you're getting an exception. Check out the api.
 
Shafian Kisna
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I tried printing out the value before inserting to Integer.parseInt, the value prints the string. However it did not work without the trim(), which was why I included the trim.

Could some one please guide me thru??

Thanks
Best Regards
SK
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You are trying to convert a double value into an integer and dividing it by two. You are using Integer.parseInt. It will throw an Exception if the parameter is not an integer. Which means that passing a double will make it throw an exception.

1. Why are you using trim() ?
2. How can you convert a double to an integer ? (ever heard of rounding issues ?)

You'll need to think of this first.
Won't post any code. Others may
 
Shafian Kisna
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satou,

don't worry, I am not asking for any codes...some pointers like this will help...
the situation here is the value output is for instance 7.58, I should represent this to string format.....how could i use rounding in here?

pls guide!!

Thanks & Best Regards
SK
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the BigDecimal class

(sorry, i'm login out. hope you'll get it right)
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this

http://forum.java.sun.com/thread.jspa?threadID=544779&messageID=2650009
 
Shafian Kisna
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks will look into that...I did try that before...
Let me look at it again.

Once again, thanks to all for your help!

Greatly appreciate the prompt replies...

Best Regards
SK
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic