Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Number format exception while converting from double to int

Thread view: 
moop™ - 23 Jan 2006 05:49 GMT
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~!
Oliver Hirschi - 23 Jan 2006 07:05 GMT
moopT <samhng@gmail.com> wrote:
> Hi,
> The code
> int n = (int) java.lang.Math.round(Double.parseDouble((String)
> args.get(0)));

use "args[0]" instead of "args.get(0)"

Signature

Oliver Hirschi
http://www.FamilyHirschi.ch

Knute Johnson - 23 Jan 2006 07:13 GMT
moop™ wrote:
> Hi,
> The code
[quoted text clipped - 16 lines]
> 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?

Signature

Knute Johnson
email s/nospam/knute/

NullBock - 23 Jan 2006 08:05 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.