Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes NumberFormatException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NumberFormatException" Watch "NumberFormatException" New topic
Author

NumberFormatException

Shafian Kisna
Ranch Hand

Joined: Oct 25, 2005
Posts: 70
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

Joined: Aug 26, 2005
Posts: 824
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.


Lack of will power has caused more failure than lack of intelligence or ability.
My Blog | Red5 Resources | Technology Update | Daily Technology Tips
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Shafian Kisna
Ranch Hand

Joined: Oct 25, 2005
Posts: 70
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

Joined: Nov 24, 2005
Posts: 14687
    
  16



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

Joined: Oct 25, 2005
Posts: 70
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Have a look at the BigDecimal class

(sorry, i'm login out. hope you'll get it right)
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
Check this

http://forum.java.sun.com/thread.jspa?threadID=544779&messageID=2650009
Shafian Kisna
Ranch Hand

Joined: Oct 25, 2005
Posts: 70
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NumberFormatException