//IntCheck1.java //howto input an int without program-terminating error //try catch blocks used import javax.swing.*; public class IntCheck1 { public static void main(String[] args) { String input; int num=0; //initialize to fake out compiler boolean goodInt; goodInt = false; //not gotten good int yet do { try { input = JOptionPane.showInputDialog( "Enter an integer" ); num = Integer.parseInt( input ); //***parseInt will throw a NumberFormatException if the parameter //can not be converted to an int. Without the catch'ing of it //it would cause termination of the program. goodInt = true; //if get this far, is a good int } catch (NumberFormatException e) { //some message to user: JOptionPane.showMessageDialog(null, "Invalid integer. Try again"); } } while (!goodInt); JOptionPane.showMessageDialog(null, "Got the good int: " + num); } }