Okay, so I've been sitting here trying to figure out a solution to get this to work:
c. After the call to the getSSN(…) method, insert a while statement that tests whether the variable ssn is not an upper or lowercase letter X.
d. Insert a statement, as you see above properly aligned, that if a successful social security number is entered, it states, “Verifying you entered:”, then concatenates the ssn entered correctly by the user.
e. Don’t forget a while statement requires a re-prompt to and additional input from the user to determine if staying in the while statement.
and all I have so far is this for code but can't figure out to basically get it to re-run the prompt over and over until the user enters "x". In the instruction it says it has to have the while loop AFTER the prompt so I'm confused. Here is my code so far:
and this:import java.util.Scanner; public class SSNValidatorApp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String ssn = Validator.getSSN(sc, " Enter a Social Security Number in this\n format (nnn-nn-nnnn) or an \"X\" to exit: "); while (!ssn.equalsIgnoreCase("x")){ System.out.println(" Verifying you entered:\t\t " + ssn); return; } System.out.println("was an x."); } }
import java.util.Scanner; public class Validator { public static String getSSN(Scanner sc, String prompt) { System.out.print(prompt); boolean isValidSSN = false; String ssn = ""; while (isValidSSN == false) { ssn = sc.nextLine(); // read entire line if (ssn.length() != 11) System.out.println("Error! Invalid Social Security Number. Try again."); else if (isNumeral(ssn.charAt(0)) & isNumeral(ssn.charAt(1)) & isNumeral(ssn.charAt(2)) & ssn.charAt(3) == '-' & isNumeral(ssn.charAt(4)) & isNumeral(ssn.charAt(5)) & ssn.charAt(6) == '-' & isNumeral(ssn.charAt(7)) & isNumeral(ssn.charAt(8)) & isNumeral(ssn.charAt(9)) & isNumeral(ssn.charAt(10)) ) isValidSSN = true; else System.out.println("Error! Invalid Social Security Number. Try again."); } return ssn; } private static boolean isNumeral(char c) { if (c == '0' | c == '1' | c == '2' | c == '3' | c == '4' | c == '5' | c == '6' | c == '7' | c == '8' | c == '9') return true; else return false; } public static String getString(Scanner sc, String prompt) { System.out.print(prompt); String s = sc.next(); // read the first string on the line sc.nextLine(); // discard any other data entered on the line return s; } public static String getLine(Scanner sc, String prompt) { System.out.print(prompt); String s = sc.nextLine(); // read the whole line return s; } public static int getInt(Scanner sc, String prompt) { boolean isValid = false; int i = 0; while (isValid == false) { System.out.print(prompt); if (sc.hasNextInt()) { i = sc.nextInt(); isValid = true; } else { System.out.println("Error! Invalid integer value. Try again."); } sc.nextLine(); // discard any other data entered on the line } return i; } public static int getInt(Scanner sc, String prompt, int min, int max) { int i = 0; boolean isValid = false; while (isValid == false) { i = getInt(sc, prompt); if (i <= min) System.out.println( "Error! Number must be greater than " + min); else if (i >= max) System.out.println( "Error! Number must be less than " + max); else isValid = true; } return i; } public static double getDouble(Scanner sc, String prompt) { boolean isValid = false; double d = 0; while (isValid == false) { System.out.print(prompt); if (sc.hasNextDouble()) { d = sc.nextDouble(); isValid = true; } else { System.out.println("Error! Invalid decimal value. Try again."); } sc.nextLine(); // discard any other data entered on the line } return d; } public static double getDouble(Scanner sc, String prompt, double min, double max) { double d = 0; boolean isValid = false; while (isValid == false) { d = getDouble(sc, prompt); if (d <= min) System.out.println( "Error! Number must be greater than " + min); else if (d >= max) System.out.println( "Error! Number must be less than " + max); else isValid = true; } return d; } }