/*COMP 182 Spring 2010
Java Exceptions Example
Jan 27, 2010
*/
import java.util.*;
public class JavaExceptionExample
{
public static void main( String[]args)
{
Scanner keyboard = new Scanner(System.in);
int x = 0;
System.out.print( "Enter an integer: " );
try
{
x = keyboard.nextInt();
}
catch( InputMismatchException e)
{
System.out.println("You need to enter an integer.Run again!" );
System.exit(1);
}
System.out.println("You have successfully entered the integer " + x);
}
}
/* ----------- Sample Run 1 ----------------------------jGRASP exec: java JavaExceptionExample
Enter an integer: 245
You have successfully entered the integer 245
----jGRASP: operation complete.
----jGRASP: operation complete.
*/
/* -------------Sample Run 2 ------------------jGRASP exec: java JavaExceptionExample
Enter an integer: qwerty1
You need to enter an integer.Run again!
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
*/