Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 29th, 2005, 11:23 AM
station station is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 12 station User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #2  
Old May 29th, 2005, 01:18 PM
tfecw tfecw is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2004
Location: Washington DC
Posts: 2,753 tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level)tfecw User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 3 Days 8 h 7 m 30 sec
Reputation Power: 1570
Send a message via AIM to 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.

Reply With Quote
  #3  
Old May 29th, 2005, 02:23 PM
station station is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 12 station User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old May 31st, 2005, 03:33 PM
Rebel103 Rebel103 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 24 Rebel103 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old May 31st, 2005, 06:54 PM
station station is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 12 station User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Integer.parseInt() NumberFormatException

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap