Page 617 - Introduction to Programming with Java: A Problem Solving Approach
P. 617
14.12 Usingthrows<exception-type>toPostponethecatch 583
/*************************************************************
*
StudentList2Driver.java
Dean & Dean
This drives StudentList2 class.
*************************************************************/
*
*
*
import java.util.Scanner;
public class StudentList2Driver
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String[] names = {"Caleb", "Izumi", "Mary", "Usha"};
StudentList2 studentList = new StudentList2(names);
int index;
boolean reenter;
studentList.display();
do
{
System.outA.praingt(o"EntPerDiFndexEnofhsatundecntetro remove: "); index = stdIn.nextInt();
try
{
}
// end StudentList2Driver
}
// end main
Sample session:
}
System.out.println(
"removed " + studentList.removeStudent(index));
reenter = false;
}
while (reenter);
studentList.display();
Caleb Izumi
Mary Usha
of student to remove: 6
Enter index
Invalid entry. Enter index of student to remove: 1
removed Izumi
Caleb Mary Usha
If there is no error, this method returns name of student removed.
catch (IndexOutOfBoundsException e)
{
}
System.out.print("Invalid entry. ");
reenter = true;
If exception is thrown in removeStudent method, this catch block catches it.
Figure 14.16 Driver for StudentList2 class