NumberFormatException

Discussion in 'Java' started by morc, Mar 2, 2006.

  1. morc

    morc Guest

    hi,

    i keep gettin a number format exception :

    java.lang.NumberFormatException: For input string: "245.0"

    This is where the error is :

    int iHigh = yCoord(4.2);

    public int yCoord(double y){
    y=350-(y*25);
    int x = Integer.parseInt(Double.toString(y));
    return x;
    }

    I can't figure out why this won't work. Can somebody please point me in
    the right direction. It would be greatly appreciated.

    Thanks alot
    -morc
    morc, Mar 2, 2006
    #1
    1. Advertising

  2. morc

    Boris Stumm Guest

    morc wrote:
    > int x = Integer.parseInt(Double.toString(y));


    Makes me think of www.thedailywtf.com. Try:
    int x = (int) y;

    > I can't figure out why this won't work. Can somebody please point me in
    > the right direction. It would be greatly appreciated.


    Read the API doc.
    Boris Stumm, Mar 2, 2006
    #2
    1. Advertising

  3. morc

    James McGill Guest

    On Thu, 2006-03-02 at 07:52 -0800, morc wrote:

    > I can't figure out why this won't work. Can somebody please point me in
    > the right direction. It would be greatly appreciated.


    int x = new Integer(new Double(y).intValue()).intValue();

    You can do it without creating the intermediate objects, but this is no
    worse than your example of creating and parsing the number from a
    string.
    James McGill, Mar 2, 2006
    #3
  4. morc

    morc Guest

    thanks it worked.

    but how come parsing it the way i did doesn't work??
    morc, Mar 2, 2006
    #4
  5. morc

    James Westby Guest

    morc wrote:
    > thanks it worked.
    >
    > but how come parsing it the way i did doesn't work??
    >

    Because 345.0 isn't an integer, it's a double where the fractional part
    happens to be zero.


    James
    James Westby, Mar 2, 2006
    #5
  6. morc wrote:
    > thanks it worked.
    >
    > but how come parsing it the way i did doesn't work??
    >


    Note that the javadoc for Integer.parseInt(String) only gives a summary.
    It defines its operation in terms of the parseInt(java.lang.String, int)
    method.

    Follow that link, or scroll up a bit, and you will find the full
    information, including the list of conditions that cause a
    NumberFormatException.

    Patricia
    Patricia Shanahan, Mar 2, 2006
    #6
  7. morc

    James McGill Guest

    On Thu, 2006-03-02 at 08:26 -0800, morc wrote:
    > thanks it worked.
    >
    > but how come parsing it the way i did doesn't work??
    >


    Because a period is an illegal character in a string that is supposed to
    represent an integer in base 10, where only the characters "0-9" and "-"
    are valid.
    James McGill, Mar 2, 2006
    #7
  8. morc

    Roedy Green Guest

    On 2 Mar 2006 07:52:41 -0800, "morc" <> wrote,
    quoted or indirectly quoted someone who said :

    >public int yCoord(double y){
    > y=350-(y*25);
    > int x = Inte


    see http://mindprod.com/applets/converter.html

    It will show you the easiest and most direct way to convert from an A
    to B.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Mar 3, 2006
    #8
  9. morc

    Roedy Green Guest

    On 2 Mar 2006 08:26:47 -0800, "morc" <> wrote,
    quoted or indirectly quoted someone who said :

    >but how come parsing it the way i did doesn't work??


    dump out the intermediate string. It should be instantly obvious.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Mar 3, 2006
    #9
  10. morc

    Roedy Green Guest

    On Thu, 02 Mar 2006 09:21:23 -0700, James McGill
    <> wrote, quoted or indirectly quoted someone
    who said :

    >int x = new Integer(new Double(y).intValue()).intValue();


    Are you just trying to convert a double to an int with truncation?

    Come now. it can be done in one operation, a converting cast.

    int x = (int) y;
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Mar 3, 2006
    #10
  11. morc

    James McGill Guest

    On Fri, 2006-03-03 at 01:07 +0000, Roedy Green wrote:
    > On Thu, 02 Mar 2006 09:21:23 -0700, James McGill
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >
    > >int x = new Integer(new Double(y).intValue()).intValue();

    >
    > Are you just trying to convert a double to an int with truncation?
    >
    > Come now. it can be done in one operation, a converting cast.
    >
    > int x = (int) y;


    I know that, but the OP seemed to be looking for help with wrapper
    types in particular.
    James McGill, Mar 3, 2006
    #11
  12. Double.toString(y) should be used to change y from double to become
    String object.
    When u do Integer.parseInt(String ...) there, the String value expected
    is from the int type. but since you have the String which originated
    from a double, The Integer.parseInt(String ..) will try to do
    Integer.parseInt(x.0). Please note that the .0 there is why you get the
    exception.

    u try do do double d = 10 and you will get error as well.

    whenever u want to change something bigger (i.e double) to be something
    smaller (i.e int), you use casting. use all these parses when you need
    to do calculation with String value. Avoid unnecessary complexities.

    thanks.
    JPractitioner, Mar 3, 2006
    #12
    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:
    975
    Bob Brown
    Jul 1, 2003
  2. June Moore
    Replies:
    3
    Views:
    28,748
    Arild Skaar
    Feb 25, 2004
  3. Replies:
    3
    Views:
    1,433
  4. gbattine
    Replies:
    4
    Views:
    3,171
    Rhino
    May 23, 2006
  5. gbattine
    Replies:
    2
    Views:
    15,925
    Thomas Weidenfeller
    May 30, 2006
Loading...

Share This Page