solving java.lang.NumberFormatException ...

Discussion in 'Java' started by June Moore, Feb 25, 2004.

  1. June Moore

    June Moore Guest

    Hi,

    I'm trying to convert String to double as follows.

    String s = "100";
    double d = new Integer(s).doubleValue();
    --> Works OK.

    String s = "100.55";
    double d = new Integer(s).doubleValue();
    --> I got java.lang.NumberFormatException.

    How do I solve this problem?

    Thanks,
    June.
    June Moore, Feb 25, 2004
    #1
    1. Advertising

  2. June Moore

    KD Guest

    Hi

    You are approaching this the wrong way. Do as follows:

    String i = "100";
    String d = "100.55";

    int intprim = Integer.parseInt(i);
    Integer intObj = new Integer(i);

    double doubleprim = Double.parseDouble(d);
    Double doubleObj = new Double(d);

    NumberFormatException is occuring because you cannot convert a real
    number to an integer.

    -Karim



    June Moore wrote:
    > Hi,
    >
    > I'm trying to convert String to double as follows.
    >
    > String s = "100";
    > double d = new Integer(s).doubleValue();
    > --> Works OK.
    >
    > String s = "100.55";
    > double d = new Integer(s).doubleValue();
    > --> I got java.lang.NumberFormatException.
    >
    > How do I solve this problem?
    >
    > Thanks,
    > June.
    KD, Feb 25, 2004
    #2
    1. Advertising

  3. June Moore wrote:
    > String s = "100.55";
    > double d = new Integer(s).doubleValue();
    > --> I got java.lang.NumberFormatException.
    >
    > How do I solve this problem?


    Gee, by not using the Integer class when you want to deal with doubles.
    Use the Double class.

    /Thomas
    Thomas Weidenfeller, Feb 25, 2004
    #3
  4. June Moore

    Arild Skaar Guest

    (June Moore) wrote:

    >Hi,
    >
    >I'm trying to convert String to double as follows.
    >
    >String s = "100";
    >double d = new Integer(s).doubleValue();
    >--> Works OK.
    >
    >String s = "100.55";
    >double d = new Integer(s).doubleValue();
    >--> I got java.lang.NumberFormatException.
    >
    >How do I solve this problem?
    >
    >Thanks,
    >June.


    Unless you want to round the number, you shouldn't use Integer in this
    scenario.
    This should work better:

    String s = "100.55";
    double d = new Double(s).doubleValue();

    Regards, Arild


    --
    Arild Skår
    Granstangen 36B
    1051 Oslo
    22302057 / 90158356
    Arild Skaar, Feb 25, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Brown
    Replies:
    0
    Views:
    976
    Bob Brown
    Jul 1, 2003
  2. vizlab
    Replies:
    3
    Views:
    7,395
    satya
    Aug 26, 2007
  3. hiwa
    Replies:
    1
    Views:
    593
    Ryan Stewart
    Dec 29, 2004
  4. gbattine
    Replies:
    2
    Views:
    15,933
    Thomas Weidenfeller
    May 30, 2006
  5. Lionel
    Replies:
    14
    Views:
    1,154
Loading...

Share This Page