Results 1 to 4 of 4
  1. #1
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Unhappy Integer.parseInt("5.843"); Error

    My error:

    java.lang.NumberFormatException: For input string: "5.843"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at test.main(test.java:50)
    Why does it not want to parse my String "5.843" to int?

    The code I use for the parsing is
    Java Code:
    valuta[s] = Integer.parseInt(((Node) lstNm.item(0)).getNodeValue());
    I've also tried replacing the code which gets the String with the value from the xml file with just the string "5.843" and it still gives the same error.

    I've googled the error but I can't seem to find an answer.

    Thank you.
    Carpe Diem
    Each day's a gift and not a given right

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Cemi View Post
    I've googled the error but I can't seem to find an answer.
    your string is a double and not an int so try

    Java Code:
    System.out.println((int)(Double.parseDouble("5.843")));

    parse the double and then cast it to an int.

  3. #3
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default

    Of course! Feel really dumb now, was kinda obvious :(

    Thank you! :D
    Carpe Diem
    Each day's a gift and not a given right

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    NumberFormatException, always explain about that. As it named, once you comes-up with such an error check the format at RUNTIME. Keep in mind, this error cannot detect at the compile time, only at RUNTIME

    Can you please mark the thread solved if you satisfied with the answer.

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. "parse error at or before "}""
    By constden in forum New To Java
    Replies: 32
    Last Post: 04-25-2009, 09:49 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •