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

Description Write a Java application that will input ids and names of students i

ID: 3571511 • Letter: D

Question

Description

Write a Java application that will input ids and names of students in a college class and at the end display them all with one line per student. Do it as described in the implementation section.

Implementation

Method Main

Input Student Count

Ask the user to input student count. Input this count within an infinite loop. Inside the infinite loop, use a try/catch statement. In the try part, issue the input and convert it to an integer using Integer.parseInt(). If the input is valid and converted to an integer, break out of the infinite loop. In the catch part, catch the NumberFormatException exception and display the message to the user “Input not a whole number” and continue with the next pass through the infinite loop.

Setup a For Loop

Setup a For loop to go around equal to the student count. Within the For loop, first input the student id in an infinite loop, then input the student’s first name inside another infinite loop and then accumulate the two in a string named studentList such that there will be one line per one student containing the student id and the name.

Input Student Id within For Loop

Input the student id using an infinite loop. Inside the infinite loop, use a try/catch statement. In the try part, call a static method validateId( ) for validating the student id. If the student id is validated, break out of the infinite loop. The try part will have two catch parts. The first catch part will catch the IdNotAWholeNumberException exception, will display to the user the message extracted from the exception object by calling its method getMessage and will return from the method main. The second catch part will catch the IdOutOfRangeException exception, will display to the user the message extracted from the exception object by calling its method getMessage.

Input Student Name within For Loop

Following inputting student id, input the student first name using an infinite loop. Inside the infinite loop, use a try/catch statement. In the try part, call a static method validateName( ) for validating the student name. If the student name is validated, break out of the infinite loop. The try part will have two catch parts. The first catch part will catch the NotSpecifiedException exception, will display to the user the message extracted from the exception object by calling its method getMessage and will return from the method main. The second catch part will catch the NotAlphabeticException exception, will display to the user the message extracted from the exception object by calling its method getMessage.

After For Loop

After the end of the For loop, display the content of string studentList which should contain the list of all the students one student per line and each line containing the student id and the first name. Then it will return from the method main and program will end normally.

Methods validateId and validName

The method validateId will throw exceptions when the id is not a whole number or is out of range. Otherwise, it will simply return to the caller. The method validName will throw exceptions when the name is empty or is not alphabetic. Otherwise, it will simply return to the caller. The main method should catch each of the exceptions and call the method getMessage( ) of the exception object to extract the message and display it to the user.

Method validateId will throw objects of the following exception classes which will all be sub classes of class Exception.

IdNotAWholeNumberException extends Exception

IdOutOfRangeException extends Exception

Method validateName will throw objects of the following exception classes which will all be sub classes of the class RuntimeException.

NotSpecifiedException extends RuntimeException

NotAlphabeticException extends RuntimeException

Sample Code

Sample code contained in the method validateId ( )

                       

try {

        idInt=Integer.parseInt (id);

}

catch (NumberFormatException ex) {

        throw new IdNotAWholeNumberException ( ) ;

}

if (idInt < 1 || idInt > 999) {

        throw new IdOutOfRangeException ( ) ;

}

else {

        return;

}

Sample code contained in the method validateName ( )

if (name.equals("")){

        throw new NotSpecifiedException ();  

}

else if(!name.matches("[a-zA-Z]+")){

        throw new NotAlphabeticException ();

}

else {

        return;

}

Sample code for exception classes

Below is the sample code for IdNotAWholeNumberException and NotAlphabeticException classes. Other exception classes can be written the same way.

public class IdNotAWholeNumberException extends Exception

{

public IdNotWholeNumberException ()

{

    super ("Id Not Whole Number");

}

}

public class NotAlphabeticException extends RuntimeException

{

public NotAlphabeticException ()

{

    super (“Not Alphabetic");

}

}

Testing

Test Run 1

Enter student count

a12

Input not a whole number

Enter student count

12.2

Input not a whole number

Enter student count

3

Enter student id

5.2

Id Not Whole Number

Enter student id

1000

Id out of range

Enter student id

1

Enter student first name

234

Name not alphabetic

Enter student first name

        

(leave it empty)

Name not specified (left empty)

Enter student first name

Jill

Enter student id

2

Enter student first name

Jack

Enter student id

3

Enter student first name

Judy

Student List

1 Jill

2 Jack

3 Judy

Explanation / Answer

Please follow the code and comments for description :

CODE :

import java.util.Scanner; // required imports for the code

public class studentData { // class to run the code

private static int sId; // instance variables
private static String sName;

public static void main(String[] args) throws IdNotAWholeNumberException, NotSpecifiedException, NotAlphabeticException { // driver method that throws the exceptions required

Scanner sc = new Scanner(System.in); // scanner class to read the data
StringBuffer studentList = new StringBuffer(); // buffer to add the data to list
int sCount; // local variables
while (true) { // iterate over infinite
try { // try catch block
System.out.print("Please Enter the Student Count : "); // prompt
sCount = Integer.parseInt(sc.next()); // read the data
break; // break the looop
} catch (NumberFormatException numEx) { // catch the exception
System.out.println("Input not a whole number."); // message
}
}
for (int i = 0; i < sCount; i++) { // iterate over the count
while (true) { // iterate over infinite
try { // try catch block
System.out.print("Please Enter the Student ID : "); // prompt
Object ob = sc.next(); // read the data
validateId(ob); // call the method to validate
break; // break the looop
} catch (IdNotAWholeNumberException | IdOutOfRangeException idNumEx) { // catch the exception
System.out.println(idNumEx.getMessage()); // message
}
}

while (true) { // iterate over infinite
try { // try catch block
System.out.print("Please Enter the Student's First Name : "); // prompt
sName = sc.next(); // read the data
validateName(sName); // call the method to validate
break; // break the looop
} catch (NotSpecifiedException | NotAlphabeticException numEx) { // catch the exception
System.out.println(numEx.getMessage()); // message
}
}
studentList.append(sId).append(" ").append(sName).append(" "); // append the data
}
System.out.println(" Student's List : "); // message
System.out.println(studentList.toString()); // print the data to console
}

public static void validateId(Object ob) throws IdNotAWholeNumberException, IdOutOfRangeException { // method to validate the id
try { // try catch block
sId = Integer.parseInt((String) ob); // parse the data
} catch (NumberFormatException ex) {
throw new IdNotAWholeNumberException(); // catch the exception
}
if (sId >= 1 && sId <= 999) { // check for th erange of the id
return; // return if true
} else {
throw new IdOutOfRangeException(); // throw the exception if any
}
}

public static void validateName(String name) throws NotSpecifiedException, NotAlphabeticException { // method to validate the name
if (name.equals("")) { // check for the null condition
throw new NotSpecifiedException(); // throw the exception if any
} else if (name.matches("[a-zA-Z]+")) { // check for the alphabets are not
return;
} else {
throw new NotAlphabeticException(); // throw the exception if any
}
}

public static class IdNotAWholeNumberException extends Exception { // exception class

public IdNotAWholeNumberException() { // constructor
super("Id Not a Whole Number."); // message
}
}

private static class IdOutOfRangeException extends Exception { // exception class

public IdOutOfRangeException() { // constructor
super("Id out of range."); // message
}
}

private static class NotSpecifiedException extends Exception { // exception class

public NotSpecifiedException() { // constructor
super("Name Not Specified (left empty)."); // message
}
}

private static class NotAlphabeticException extends Exception { // exception class

public NotAlphabeticException() { // constructor
super("Name is Not Alphabetic."); // message
}
}
}

OUTPUT :

Please Enter the Student Count : a
Input not a whole number.
Please Enter the Student Count : 1.2
Input not a whole number.
Please Enter the Student Count : 2
Please Enter the Student ID : a12
Id Not a Whole Number.
Please Enter the Student ID : a
Id Not a Whole Number.
Please Enter the Student ID : 1000
Id out of range.
Please Enter the Student ID : 1
Please Enter the Student's First Name : a1
Name is Not Alphabetic.
Please Enter the Student's First Name : John
Please Enter the Student ID : 2
Please Enter the Student's First Name : Jack

Student's List :
1 John
2 Jack


Hope this is helpful.