The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Integer.parseInt() NumberFormatException
Discuss Integer.parseInt() NumberFormatException in the Java Help forum on Dev Shed. Integer.parseInt() NumberFormatException Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 29th, 2005, 11:23 AM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 12
Time spent in forums: 5 h 53 m 6 sec
Reputation 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)
|

May 29th, 2005, 01:18 PM
|
|
Contributing User
|
|
Join Date: Nov 2004
Location: Washington DC
|
|
|
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.
|

May 29th, 2005, 02:23 PM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 12
Time spent in forums: 5 h 53 m 6 sec
Reputation Power: 0
|
|
Quote: | 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? 
|

May 31st, 2005, 03:33 PM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 24
Time spent in forums: 15 h 29 m 34 sec
Reputation 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
|

May 31st, 2005, 06:54 PM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 12
Time spent in forums: 5 h 53 m 6 sec
Reputation Power: 0
|
|
Quote: | 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 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|