Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: java-Exception

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java-Exception

    Hi there,

    Anyone knows how to solve this error, it only happens when I leave the user input field blank.

    java.lang.NumberFormatException: For input string: ""
    java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    java.lang.Integer.parseInt(Integer.java:470)
    java.lang.Integer.parseInt(Integer.java:499)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: java-Exception

    Read the API for the methods you are using - it describes why an exception would be thrown.
    Integer (Java Platform SE 6)

    And for future reference, it help s to place context by posting an SSCCE

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java-Exception

    thanks for the link, but my error is till not solve. After reading, "NumberFormatException - if the string does not contain a parsable integer." because the user input is left blank.

    Any more suggestions how to solve it? thanks

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: java-Exception

    Look over the code you failed to provide. Decide where in the code you have tried to pull an integer value from an empty string. Figure out a way to prevent the string from not containing an integer before the error occurs.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: java-Exception

    Quote Originally Posted by Brane View Post
    "NumberFormatException - if the string does not contain a parsable integer." because the user input is left blank
    Exactly. Is an empty string a parsable integer? No. Validate user input if you expect an integer, or catch the exception.

    And please, when posting code, wrap it in the code tags. The forum rules clearly explains how to do this.

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java-Exception

    Exactly. Is an empty string a parsable integer? No. Validate user input if you expect an integer, or catch the exception.

    And please, when posting code, wrap it in the code tags. The forum rules clearly explains how to do this.
    Thanks copeg, Can you give an example? as try to solve it by:

    if(studentid != null && !studentid.equals("")){
    int studentIdNum = Integer.parseInt(studentid);
    }
    else{
    //no number, deal with it
    throw new Exception("Invalid student Id!");
    }
    it doesn't work. thanks

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: java-Exception

    it doesn't work
    Please define, "it doesn't work" (to be blunt: that's about as much information to us as us saying to you "then fix it")

  8. #8
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java-Exception

    Please define, "it doesn't work" (to be blunt: that's about as much information to us as us saying to you "then fix it")
    I still get the following error when the user input field is left blank:

    java.lang.NumberFormatException: For input string: ""
    java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    java.lang.Integer.parseInt(Integer.java:470)
    java.lang.Integer.parseInt(Integer.java:499)
    Is there anything wrong in my if statement?

    if(studentid != null && !studentid.equals("")){
    int studentIdNum = Integer.parseInt(studentid);
    }
    else{
    //no number, deal with it
    throw new Exception("Invalid student Id!");
    }
    thanks

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: java-Exception

    Quote Originally Posted by Brane View Post
    Is there anything wrong in my if statement?
    In that short clip posted, no. But, in the code you posted in post 5, your variable is called 'StudentID', not 'sudentid'. Post an SSCCE and we can all be on the same page when referring to code.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: java-Exception

    Please wrap the code in code tags, not quote.
    Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: java-Exception

    You forgot to format the code posted in #11. The statements should NOT all start in the first column. There should be indentations for nested code within {}s etc

    Does the code posted in post#11 get the error? Can you post the full text of the error message?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: java-Exception

    That is not an SSCCE. That being said, there are two calls to parseInt - one within your validation, one above. You should make use of printing out variable values (for debugging) and exception stack traces, both of which help you determine not only where but why exceptions are thrown.

Similar Threads

  1. java.lang.NoClassDefFoundError exception
    By ayuda96 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 23rd, 2012, 11:12 PM
  2. Java Null Pointer Exception
    By destructobob in forum Exceptions
    Replies: 5
    Last Post: December 9th, 2011, 11:53 AM
  3. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  4. java.net.URL Access Exception
    By MistaWizurd in forum Java Networking
    Replies: 5
    Last Post: April 3rd, 2011, 02:18 AM
  5. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM