Java if statement check social security number format

Question

We would like to 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.

Here are sample runs:

Enter a SSN: 232-23-1234 
232-23-1234 is a valid social security number 

Enter a SSN: 23-23-1234 
23-23-1234 is an invalid social security number 
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a SSN: ");
    String entry = input.nextLine();

    String result = entry + " is a valid Social Security number";
    if (!isValidEntry(entry)) {
      result = result.replace("a valid", "an invalid");
    }//from  w w  w .  j a v a  2  s  . com

    System.out.println(result);
  }

  private static boolean isValidEntry(String ssn) {
    // 1. Make sure SSN contains 3 chunks of characters separated by hyphens
    // 2. Make sure the lengths of each chunk are correct (3-2-4)
    // 3. Make sure that each chunk is an integer
    
    // your code here
    
    return valid;
  }

  private static boolean isInteger(String s) {
    boolean integer = true;
    try {
      Integer.parseInt(s);
    } catch (Exception e) {
      integer = false;
    }
    return integer;
  }
}



import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a SSN: ");
    String entry = input.nextLine();

    String result = entry + " is a valid Social Security number";
    if (!isValidEntry(entry)) {
      result = result.replace("a valid", "an invalid");
    }

    System.out.println(result);
  }

  private static boolean isValidEntry(String ssn) {
    // 1. Make sure SSN contains 3 chunks of characters separated by hyphens
    // 2. Make sure the lengths of each chunk are correct (3-2-4)
    // 3. Make sure that each chunk is an integer
    String[] ssnSplit = ssn.split("-");
    boolean valid = true;
    if (ssnSplit.length != 3) {
      valid = false;
    } else if (ssnSplit[0].length() != 3 ||
               ssnSplit[1].length() != 2 ||
               ssnSplit[2].length() != 4) {
      valid = false;
    } else if (!isInteger(ssnSplit[0]) || !isInteger(ssnSplit[1]) ||
               !isInteger(ssnSplit[2])) {
      valid = false;
    }
    return valid;
  }

  private static boolean isInteger(String s) {
    boolean integer = true;
    try {
      Integer.parseInt(s);
    } catch (Exception e) {
      integer = false;
    }
    return integer;
  }
}



PreviousNext

Related