View Single Post
  #3 (permalink)  
Old 01-26-2008, 07:34 AM
CaptainMorgan's Avatar
CaptainMorgan CaptainMorgan is offline
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Your input is related to Integer and its method(s), namely parseInt(). And you want to catch the NumberFormatException because this is thrown if the String cannot be parsed into an int.

Code:
import javax.swing.JOptionPane; public class Chapter8Q2 { public static void main ( String[] args ) { Integer age = new Integer(0); // initialization int year; String message1 = "Please enter your age in years" + "\n"; String message3 = "Please enter positive integer values only"+ "\n"; String message4 = "Please enter in numbers, not letter"+ "\n"; String temp ,outputMessage ; try{ temp = JOptionPane.showInputDialog(message1); year= age.parseInt(temp); if( year< 0) throw new IllegalArgumentException(); outputMessage = "Your age is "+ year+ " years"+ "\n"; JOptionPane.showMessageDialog(null, outputMessage); }//end try catch( NumberFormatException e) // can't parse the String into an int { JOptionPane.showMessageDialog(null, message4); } catch( IllegalArgumentException e) //negative number { JOptionPane.showMessageDialog(null, message3); }//end catch System.exit(0); }//main }//class
Hope this helps.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Reply With Quote