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

This assignment deals with string manipulation problems. Use only the charAt, eq

ID: 3663372 • Letter: T

Question

This assignment deals with string manipulation problems.

   Use only the charAt, equals, length, substring, and toUpperCase methods of the String class (see the Java API for details) in your solutions.

   The methods you’re writing for this assignment simply make use of String objects. You’re not actually writing new methods for Java’s built-in String class! Thus, all your methods can be declared as static, just like most of the ones you wrote in COMP 1900.

   You’ve set up a My Little Pony fan website that collects registration information from users via a form. This info includes the user’s social security number, and you want to make sure that this number is valid.

Write a method isValidSSN(String n) that returns whether or not the string n is in valid SSN format. Assume a “valid” SSN is something in the form DDD-DD-DDDD, DDD DD DDDD, or DDDDDDDDD, where D is a digit from 0-9. These formats cannot be mixed. (If you happen to be familiar with regular expressions, do not use them for this problem. That would be too easy )

Examples of valid SSNs:   123-45-6789
               123 45 6789
               123456789

Examples of invalid SSNs:   123-45 6789
               12345 6789
               1SS-N4-SALE

Explanation / Answer

/**The java program SSNValidiity that checks for valid and invalid
* SSN numbers are in the format of DDD-DD-DDDD or DDD DD DDDD
* or DDDDDDDDD .
* The program demonstrates valid and invalid ssn numbers
* and prints results.
* */
//SSNValidiity.java
public class SSNValidiity
{
   public static void main(String[] args)
   {          
       String n="123-45-6789";

       System.out.println("Valid SSN");
       if(isValidSSN(n))
           System.out.println(n);
       else
           System.out.println("Invalid SSN");

       n="123 45 6789";      
       if(isValidSSN(n))
           System.out.println(n);
       else
           System.out.println("Invalid SSN");

       n="123456789";      
       if(isValidSSN(n))
           System.out.println(n);
       else
           System.out.println("Invalid SSN");
      
       n="123-45 6789";      
       if(isValidSSN(n))
           System.out.println(n);
       else
           System.out.println(n+" is invalid SSN");

       n="12345 6789";          
       if(isValidSSN(n))
           System.out.println(n);
       else
           System.out.println(n+" is invalid SSN");

       n="1SS-N4-SALE";      
       if(isValidSSN(n))
           System.out.println(n);
       else
           System.out.println(n+" is invalid SSN");

   }

   /*The method isValid takes an string of formatted SSN and retuns
   * true if the string is of valid SSN format otherwise returns false*/
   private static boolean isValidSSN(String n)
   {
       boolean valid=false;
       String three;
       String two;
       String four;
       //Get size
       int size=n.length();

       //Check if size 11
       if(size==11)
       {
           //Get strings
           three=n.substring(0, 3);
           two=n.substring(4, 6);
           four=n.substring(7, n.length());
           if((n.charAt(3)=='-' && n.charAt(6)=='-'))
               valid=true;
           else if((n.charAt(3)==' ' && n.charAt(6)==' '))
               valid=true;
          
           if(!validDigits(three) &&!validDigits(two) &&!validDigits(four))
               valid=false;


       }
       //Check size is 9
       else if(size==9)
       {
           //Get strings
           three=n.substring(0, 3);
           two=n.substring(3, 5);
           four=n.substring(5, n.length());
           //Check if characters are digits
           if(!validDigits(three) &&!validDigits(two) &&!validDigits(four))
               valid=false;
       }
       else
           valid=false;
       //return valid
       return valid;
   }

   /* Helper method that takes a string and returns true
       if letters are digit otherwise returns false */
   private static boolean validDigits(String digits)
   {
       boolean valid=true;
       for (int i = 0; i < digits.length() && valid; i++)
       {
           if(!Character.isDigit(digits.charAt(i)))
               valid=false;

       }
       return valid;
   }
}//end of the class

------------------------------------------------------------------------------------------------------------------------

Sample output:

Valid SSN
123-45-6789
123 45 6789
Invalid SSN
123-45 6789 is invalid SSN
12345 6789 is invalid SSN
1SS-N4-SALE is invalid SSN

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