//tests a customer number to verify that it is in the proper format //can also use regular expression to check, refer to java.util.regex package, not covered here import javax.swing.JOptionPane; public class eg20_CustomerNumber { public static void main(String[] args) { String input; //get a customer number input = JOptionPane.showInputDialog("Enter a customer number in the form LLLNNNN\n (LLL = letters and NNN = numbers)"); if (isValid(input)) //static method defined below { JOptionPane.showMessageDialog(null, "That's a valid customer number."); } else { JOptionPane.showMessageDialog(null, "That is not the proper format of a customer number.\n Here is an example: ABC1234"); } } private static boolean isValid(String custNumber) { boolean goodSoFar = true; //flag int i=0; //explain logic here //test the length if (custNumber.length() !=7) goodSoFar = false; //first 3 characters for letters while (goodSoFar && i<3) { if (!Character.isLetter(custNumber.charAt(i))) goodSoFar = false; System.out.println(custNumber.charAt(i)+" => "+goodSoFar); i++; } //last four characters for digits while (goodSoFar && i<7) { if (!Character.isDigit(custNumber.charAt(i))) goodSoFar = false; System.out.println(custNumber.charAt(i)+" => "+goodSoFar); i++; } return goodSoFar; } }