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

Using Java, I need to write a program that prompts the user to enter a Social Se

ID: 3926720 • Letter: U

Question

Using Java, I need 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. So If I entered the number 232-23-5435 the program would display the output "232-23-5435 is a valid social security number". If I entered the number 23-23-5435, the program would display the output "23-23-5435 is an invalid social security number". I am enrolled in a Computer Programming I course so the easier this is to understand, the better.

Explanation / Answer

Check.java


import java.util.Scanner;

public class Check {

   public static void main(String[] args) {
           Scanner scan = new Scanner(System.in);
           System.out.println("Ener SSN No:");
           String ssn = scan.nextLine();
           String status = isValidSSN(ssn);
           System.out.println(ssn+" is "+status);
   }
   public static String isValidSSN(String s ){
       int length = s.length();
       if(length == 11 || length == 9){
           if(length == 11) {
           if(s.contains("-") || s.contains(" ")){
               String each[] = null;
               if(s.contains("-")){
                   each = s.split("-");
               }
               else{
                   each = s.split(" ");
               }
              
               if(each.length == 3){
                   if(each[0].length() == 3 && each[1].length() == 2 && each[2].length() == 4){
                       return "a valid social security number";
                   }
                   else{
                       return "an invalid social security number";
                   }
               }
               else{
                   return "an invalid social security number";
               }
           }
           else{
               return "an invalid social security number";
           }
       }
       else {
           int flag = 0;
           for(int i=0; i<length; i++){
               if(!Character.isDigit(s.charAt(i))){
                   flag = 1;
                   break;
               }
           }
           if(flag == 1){
               return "an invalid social security number";
           }
           else{
               return "a valid social security number";
           }
       }
       }
       else{
           return "an invalid social security number";
       }
   }
}

Output:

Ener SSN No:
232-23-5435
232-23-5435 is a valid social security number

Ener SSN No:
23-23-5435
23-23-5435 is an invalid social security number

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