Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add a method named getSSN to the Validator class that accepts and validates a so

ID: 3621857 • Letter: A

Question

Add a method named getSSN to the Validator class that accepts and validates a social security number. This method should accept a Scanner object and a string that will be displayed to the user as a prompt. After it accepts the social security number, this method should validate the entry by checking that the number consists of three numeric digits, followed by a hyphen and two numeric digits, followed by a hyphen and four numeric digits. If the user's entry doesn't conform to this format, the method should display an error message and ask the user to enter the number again.

**I am struggling with knowing how to validate the format, the numbers and hyphen combinations. If someone could help me understand this I would greatly appreciate it!!**

Here is my Validator class...
import java.util.Scanner;

public class Validator
{
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;
}
}

Here is my application...

import java.util.Scanner;

public class SSNValidatorApp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String ssn = Validator.getLine(sc, "Enter a Social Security Number: ");
System.out.println("You entered: " + ssn);
}
}


** Thanks again for your help**

Explanation / Answer

public static int getSSN(Scanner sc, String prompt) { String ssn; StringBuilder sb; boolean isValid = false; while (isValid == false) { ssn = getString(sc, prompt); boolean firstGroup = false; //check the first 3 digits: if (ssn.charAt(0) >= 45 && ssn.charAt(0) = 45 && ssn.charAt(1) = 45 && ssn.charAt(2) = 45 && ssn.charAt(4) = 45 && ssn.charAt(5) = 45 && ssn.charAt(6) = 45 && ssn.charAt(8) = 45 && ssn.charAt(9) = 45 && ssn.charAt(10)
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote