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 {

Edited 6 Years Ago by jems5: n/a

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 {

How I see this is, that if you really need to do it by Exception handling then you can coin it this way in the try block:

if (OccupantsInput > MAX_OCCUPANTS) 
{
   if (OccupantsInput > MAX_OCCUPANTS)
   {
       OccupantsInput = OccupantsInput/0;
   }
//which will be caught by the ArithmeticException you have
}

Please remember to click " Mark this Thread as Solved" down there if this reply sorts out your problem.

Thanks!

Edited 6 Years Ago by dangari: n/a

Here the problem is with setting the limits for the condition of the if statements

if((OccupantsInput > MAX_OCCUPANTS) || (OccupantsInput < 0)){
         System.out.println("Occupancy limit was exceeded");
}

Hope this code will work perfectly.

Edited 6 Years Ago by sincerelibran: Missed one bracket

How I see this is, that if you really need to do it by Exception handling then you can coin it this way in the try block:

if (OccupantsInput > MAX_OCCUPANTS) 
{
   if (OccupantsInput > MAX_OCCUPANTS)
   {
       OccupantsInput = OccupantsInput/0;
   }
//which will be caught by the ArithmeticException you have
}

Please remember to click " Mark this Thread as Solved" down there if this reply sorts out your problem.

Thanks!

Remember also that using ArithmeticException isn't the only way to achieve what you want here. You can simply do this to avoid Exception handling (for this case only):

if (OccupantsInput > MAX_OCCUPANTS) 
{
       System.out.println("Maximum allowable limit has been exceeded!Please retry.");
       scanner.nextLine();//next line (in case you used Scanner above somewhere)
}

This should be fine because if the case happens program prints to screen and the user is allowed to enter afresh.

Here the problem is with setting the limits for the condition of the if statements

if((OccupantsInput > MAX_OCCUPANTS) || (OccupantsInput < 0)){
         System.out.println("Occupancy limit was exceeded");
}

Hope this code will work perfectly.

Thanks for the suggestion but forum participant Dangari suggestion works best. I had used your suggestion to check to make sure I could relate the final int variable to the user input but I really needed to catch the error. Indeed what you have suggested does throw the exception needed but I need it to be caught so the program stalls until the proper input is registered. Dangari suggested using exception which worked beautifully...i.e.:

if (userInput > final int) {
userInput = userInput/0;
}

Thanks...I will close the question as being resolved.
Jems

How I see this is, that if you really need to do it by Exception handling then you can coin it this way in the try block:

if (OccupantsInput > MAX_OCCUPANTS) 
{
   if (OccupantsInput > MAX_OCCUPANTS)
   {
       OccupantsInput = OccupantsInput/0;
   }
//which will be caught by the ArithmeticException you have
}

Please remember to click " Mark this Thread as Solved" down there if this reply sorts out your problem.

Thanks!

This worked beautifully, thank you very much!

This worked beautifully, thank you very much!

Thank you jems5. I am humbled and glad to be of help.

This worked beautifully, thank you very much!

Thank you jems5. I am humbled and glad to be of help.

This question has already been answered. Start a new discussion instead.