This program will ask the user for a person's name and social security number. The program will then check to see if the social security number is valid. An exception will be thrown if an invalid SSN is entered.
SocSecException.java file gives an example of custom exception class. In java, you can use pre- defined exceptions from J2SE library (check http://docs.oracle.com/javase/7/docs/api/ ), or write a subclass of a pre-defined exceptions. In the subclass, you simply call super class and add specific messages. You do not need to modify the SocSecException.java file.
Modify SocSecProcessor.java file to handle an exception:
1. In the main method read a name and social security number from the user as Strings. The main method should contain a try-catch statement. This statement tries to check if the social security number is valid by using the method isValid. If the social security number is valid, it prints the name and social security number. If a SocSecException is thrown, it should catch it and print out the name, social security number entered, and an associated error message indicating why the social security number is invalid. A loop should be used to allow the user to continue until the user indicates that they do not want to continue.
2. The static isValid method:
3. Compile, debug, and run your program. Sample output is shown below with user input in bold.
OUTPUT (boldface is user input)
Name? Sam Sly
SSN? 333-00-999
Invalid the social security number, wrong number of characters
Continue? y
Name? George Washington
SSN? 123-45-6789
George Washington 123-45-6789 is valid
Continue? y
Name? Dudley Doright
SSN? 222-00-999o
Invalid the social security number, contains a character that is
not a digit
Continue? y
Name? Jane Doe
SSN? 333-333-333
Invalid the social security number, dashes at wrong positions
Continue? n