Number format exception while converting from double to int

  • Thread starter =?iso-8859-1?B?bW9vcJk=?=
  • Start date
?

=?iso-8859-1?B?bW9vcJk=?=

Hi,
The code
int n = (int) java.lang.Math.round(Double.parseDouble((String)
args.get(0)));
r = (int) java.lang.MathException in thread "main"

resulted in the following ex:
java.lang.NumberFormatException: For input string: "5.0"
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)

The args.get(0) represents "5.0" as input. I have tried the most common
way
int n = Integer.parseInt((String) args.get(0));
but eventually failed and later evolved to the version as at the front
of this post. BTW, I use JAVA REFLECTION to do so. The code is inside a
class which is called by a reflection procedure.
What is it going wrong? Thx~!
 
K

Knute Johnson

moop™ said:
Hi,
The code
int n = (int) java.lang.Math.round(Double.parseDouble((String)
args.get(0)));
r = (int) java.lang.MathException in thread "main"

resulted in the following ex:
java.lang.NumberFormatException: For input string: "5.0"
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)

The args.get(0) represents "5.0" as input. I have tried the most common
way
int n = Integer.parseInt((String) args.get(0));
but eventually failed and later evolved to the version as at the front
of this post. BTW, I use JAVA REFLECTION to do so. The code is inside a
class which is called by a reflection procedure.
What is it going wrong? Thx~!

Well you can't Integer.parseInt() a string with a decimal point in it.
The Double.parseDouble() ought to work but are you sure that you don't
have any spaces in it? Are you sure that (String)args.get(0) is
returning a string?
 
N

NullBock

It looks to me like you have a problem with your reflection:
at java.lang.Integer.parseInt(Integer.java:456) !! The *Integer* class is parsing it!

Are you sure you're not dynamically loading/using a stale version of
the class file?

Walter Gildersleeve
Freiburg, Germany

______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top