My program requires that the user input values for an array do not exceed a declared final int variable. To control this I wrote this code to catch and throw exceptions in order to prevent computer from crashing the program. There are two conditions to be handled.
(1) Exceeding array - in this case 10
(2) Cumulative values not to exceed 20 - in this case maximum occupants
I am catching the array exception but non responsive to number (2). The code segment is below:
final int MAX_OCCUPANTS = 20; // Total occupants in array cannot exceed 20
int ApartmentInput; // Text field user I/O apartment (array position) input
int OccupantsInput; // Text field user I/O occupants input
double sum = 0.0; // Sum variable
try {
occupants[ApartmentInput] = OccupantsInput; // Reads in value into array position
sum += occupants[ApartmentInput];
// When the next 3 blocked code lines are unblocked then the system responds alerting an input in excess of MAX_OCCUPANTS but this is not what is needed. The error needs to be caught.
//if ((OccupantsInput < MAX_OCCUPANTS) || (OccupantsInput > MAX_OCCUPANTS)) {
//System.out.println("Occupancy limit was exceeded");
//}
}
catch (ArrayIndexOutOfBoundsException ioErr){ // Works correctly no problem here
System.out.println("You have exceeded array range");
}
catch (ArithmeticException ioErr){ // Not even sure if this is the proper exception
System.out.println("Occupancy limit was exceeded"); // Really want to catch and report exception here but is not working correctly
}
finally {