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.
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.