#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    12
    Rep Power
    0

    Integer.parseInt() NumberFormatException


    I'm having trouble using Integer.parseInt(), it's giving me a NumberFormatException, etc. This code is suppose to read the user input and put it in a string, then parse the integer from the string. Not sure what the problem is, here is my code. Can someone tell me what's wrong with it?

    Code:
    import java.util.Scanner;
    
    public class Driver
    {
    public static void main(String[]args)
    {
    
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Key in sentence with number");
    String first = scan.next();
    
    System.out.println("Key in sentence with number");
    String sec = scan.next();
    
    int a = Integer.parseInt(first);
    int b = Integer.parseInt(sec);
    
    System.out.println("sum is " + (a + b));
    
    }
    }
    
    //end of program
    this is the output

    Key in sentence with number
    this number is 1
    Key in sentence with number
    Exception in thread "main" java.lang.NumberFormatException: For input string: "this"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,756
    Rep Power
    1571
    Hmm, well NUmberFormatExceptions are thrown when your string is something other then a number. IF you do something like Integer.parseInt("FOO"), it will throw the excpetion.

    That being said. I copy and pased your code and it seems to work fine. Instead of typing "This number is 1" when prompted for input. Type in only numbers.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    12
    Rep Power
    0
    Originally Posted by tfecw
    Hmm, well NUmberFormatExceptions are thrown when your string is something other then a number. IF you do something like Integer.parseInt("FOO"), it will throw the excpetion.

    That being said. I copy and pased your code and it seems to work fine. Instead of typing "This number is 1" when prompted for input. Type in only numbers.
    but isn't the Integer.parseInt(String str) suppose to take a string as a parameter then get the integer out?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    24
    Rep Power
    0
    It will take a string or name parameter that has a numeric value and convert it to the numeric value. When you are inputing values from the keyboard as you are above as strings it still must be something that can be converted to an int, i.e. "12". Which is why tfcew told you to just input numbers and the code works.
    Example:
    HTML Parameter: <param name="UserID" value="1">
    Java String to Int: int x=Integer.parseInt(getParameter("UserID")); // value will = 1

    Here is something that may help you understand: http://www.javaworld.com/javaworld/jw-10-2002/jw-1004-java101.html
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    12
    Rep Power
    0
    Originally Posted by Rebel103
    It will take a string or name parameter that has a numeric value and convert it to the numeric value. When you are inputing values from the keyboard as you are above as strings it still must be something that can be converted to an int, i.e. "12". Which is why tfcew told you to just input numbers and the code works.
    Example:
    HTML Parameter: <param name="UserID" value="1">
    Java String to Int: int x=Integer.parseInt(getParameter("UserID")); // value will = 1

    Here is something that may help you understand: http://www.javaworld.com/javaworld/jw-10-2002/jw-1004-java101.html
    okay thanks i got it now

IMN logo majestic logo threadwatch logo seochat tools logo