Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Unhappy Exception java.lang.NumberFormatException

    I was testing one of my starter programs as an attempt to see if I can get a program working, everything went fine until I pressed in !stop to stop putting in numbers and start calculating. My program is an Averaging program which takes a set of data, increments the value of entries every time someone puts in a number and stores the new number in an ArrayList<Integer>. Here's what I was doing:
    Welcome to the Averaging Program.To verify you made this program, please enter a 4-digit pin: 0000
    Password accepted.

    I will be asking you to input values
    Please insert a number, or to stop, type !stop.10
    Please insert a number, or to stop, type !stop.10
    Please insert a number, or to stop, type !stop.10
    Please insert a number, or to stop, type !stop.!stop
    Exception in thread "main" java.lang.NumberFormatException: For input string: "!stop"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Averaging.loopInsert(Averaging.java:38)
    at Averaging.verify(Averaging.java:26)
    at Averaging.main(Averaging.java:20)
    I have already had knowledge of translation stacktracing, and it tells me line 20, 26, and 38 are broken.

    My main class is on Pastebin at [Java] java.lang.NumberFormatException 1 (Averaging Program) - Pastebin.com

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Exception java.lang.NumberFormatException

    You cannot compare strings using:
    Java Code:
    if(next == "!stop")
    Depending on your needs, the String class provides many ways to compare strings. In your case, all you need is:
    Java Code:
    if(next.equals("!stop"))

  3. #3
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Exception java.lang.NumberFormatException

    I should have added a bit more. What do you think happens when you test incorrectly and it always evaluated to false, sending you down the else path? One way to debug this would be to print out what next is at the point of the parseInt() call. Even than may not have helped you to realize that how you tested the contents of next was incorrect, but it would have helped to zero in on the problem....like why on earth am I ending up in that else when my input was clearly !stop?

  4. #4
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Exception java.lang.NumberFormatException

    Your first post:

    Thanks for this.

    Your second post:

    I wanted to do the input !stop so the user using the program can stop entering numbers into an ArrayList<Integer> and start calculating.

    This is only my first piece of software I have created. I'm still trying to learn how to make certain stuff (like IO managing [I know some, and can do YML file making in the program], and such).

  5. #5
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Exception java.lang.NumberFormatException

    Quote Originally Posted by JustAHorriblePlayer View Post
    Your first post:
    I wanted to do the input !stop so the user using the program can stop entering numbers into an ArrayList<Integer> and start calculating.

    This is only my first piece of software I have created. I'm still trying to learn how to make certain stuff (like IO managing [I know some, and can do YML file making in the program], and such).
    I understand, I'm asking questions in order to help you think about what you did so you better understand.

Similar Threads

  1. Replies: 14
    Last Post: 04-08-2011, 07:01 PM
  2. Replies: 5
    Last Post: 04-12-2010, 04:39 PM
  3. Replies: 3
    Last Post: 06-07-2009, 01:14 AM
  4. Exception java.lang.NumberFormatException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-24-2009, 07:27 AM
  5. java.lang.NumberFormatException: null
    By Eric in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-05-2007, 06:31 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
  •