Write a program that 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. You will be creating your own exception class in this program. You will also create a driver program that will use the exception class. Within the driver program, you will include a static method that throws the exception.

Respuesta :

Answer:

See explaination for the program code

Explanation:

//SocSecProcessor.java:

import java.util.Scanner;

public class SocSecProcessor

{

public static void main (String [] args)

{

Scanner keyboard = new Scanner (System.in);

String name;

String socSecNumber;

String response;

char answer = 'Y';

while (Character.toUpperCase(answer) == 'Y')

{

try

{

// Task #2 step 1 - To do:

// promote user to enter name and ssn number.

// save the input to variable name and SocSecNumber. Such as:

// System.out.print("Name? ");

// name = keyboard.nextLine();

// validate SSN number by calling isValid(socSecNumber).

// output the checking result.

System.out.print("Name?");

name = keyboard.nextLine();

System.out.print("SSN?");

socSecNumber = keyboard.nextLine();

if(isValid(socSecNumber)){

System.out.println(name + " " + socSecNumber + "is Valid");

}

}

catch (SocSecException e) // To do: catch the SocSecException

{

System.out.println(e.getMessage());

}

System.out.print("Continue? ");

response = keyboard.nextLine();

answer = response.charAt(0);

}

}

private static boolean isValid(String number)throws SocSecException

{

boolean goodSoFar = true;

int index = 0;

// To do: Task #2 step 2

// 1. check the SSN length. Throw SocSecException if it is not 11

if (number.length() != 11)

{

throw new SocSecException("wrong number of characters ");

}

for( index=0;index<number.length();++index){

if(index==3 || index==6){

if (number.charAt(index) != '-'){

throw new SocSecException("dashes at wrong positions");

}

}else if (!Character.isDigit(number.charAt(index))){

throw new SocSecException("contains a character that is not a digit");

}

}

// 2. check the two "-" are in right position. Throw SocSecException if it is not

// if (number.charAt(3) != '-')

// 3. check other position that should be digits. Throw SocSecException if it is not

// if (!Character.isDigit(number.charAt(index)))

return goodSoFar;

}

}

See attachment

Ver imagen kendrich
ACCESS MORE