Validate SSN

The problem

Write a program that prompts the user to enter a social Security number in the format DDD-DD-DDDD, where D is a digit. Your program should check whether the input is valid.

Breaking it down

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Format: DDD-DD-DDDD");
    System.out.print("Enter SSN: ");
    String ssn = input.nextLine();
    input.close();

    boolean isSSNValidFormat = ssn
            .matches("(^\d{3}-?\d{2}-?\d{4}$|^XXX-XX-XXXX$)");

    if (isSSNValidFormat) {
        System.out.println(ssn + " is a valid social security number.");
    } else {
        System.out.println(ssn + " is a INVALID social security number.");
    }
}

Output

Format: DDD-DD-DDDD
Enter SSN: 123-45-1234
123-45-1234 is a valid social security number.


Format: DDD-DD-DDDD
Enter SSN: 232-23223-2323-
232-23223-2323- is a INVALID social security number.