I tried this many times and still cannot get through it.

ZyBooks My library > 11_ 145. Intro to Software Development home > 6.9: Handling exceptions PARTICIPATION ACTIVITY 6.9.5: Handling input exceptions: restaurant max occupancy tracker. Arrange the following lines to make a program that determines when the number of people in a restaurant equals or exceeds 10 occupants. The program continually gets the number of people entering or leaving the restaurant. Ex: 2 means two people entered, and -3 means three people left. After each input, the program outputs the number of people in the restaurant. Once the number of people in the restaurant equals or exceeds 10, the program exits. If an InputMismatchException exception occurs, the program should get and discard a single string from input. Ex: The input “2 abc 8” should result in 10 occupants. Not all lines are used in the solution. Mouse: Drag/drop Keyboard: Grab/release Spacebar (or Enter). Move +00D Cancel Esc Unused System.out.println(“Error”); } if (totalNumPeople < 0)=”” {=”” totalnumpeople=”0;” }=”” catch=”” (inputmismatchexception=”” e)=”” {=”” system.out.println(“occupancy:=”” “=”” +=”” totalnumpeople);=”” }=”” totalnumpeople=”” +=”” scnr.nextint=”” ();=”” catch=”” {=”” try=”” {=”” }=”” while=”” (totalnumpeople=””>< maxnumpeople)=”” {=”” maxoccupancy=”” tracker.java=”” e=”” zybooks=”” catalog=”” import=”” java.util.scanner;=”” import=”” java.util.=”” inputmismatchexception;=”” }=”” load=”” default=”” template…=”” public=”” class=”” maxoccupancytracker=”” {=”” public=”” static=”” void=”” main=”” (string[]=”” args)=”” {=”” scanner=”” scnr=”new” scanner(system.in);=”” int=”” maxnumpeople=”10;” int=”” totalnumpeople=”0;” check=”” x=”” failed=”” to=”” compile=”” maxoccupancytracker=”” java:17:=”” error:=”” ‘catch’=”” without=”” ‘try!=”” system.out.println(“we’re=”” full!”);=”” help/faq=”” sydney=””>
The correct answer and explanation is:
To create a functioning Java program for the described “restaurant max occupancy tracker” using exception handling, you need to properly organize the given code fragments. Below is the complete and corrected code:
import java.util.Scanner;
import java.util.InputMismatchException;
public class MaxOccupancyTracker {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int maxNumPeople = 10;
int totalNumPeople = 0;
while (totalNumPeople < maxNumPeople) {
try {
System.out.println("Enter the number of people entering or leaving:");
totalNumPeople += scnr.nextInt();
if (totalNumPeople < 0) {
totalNumPeople = 0; // Prevent negative occupancy
}
System.out.println("Occupancy: " + totalNumPeople);
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Please enter an integer.");
scnr.next(); // Discard invalid input
}
}
System.out.println("We're full!");
scnr.close();
}
}
Explanation of the Code:
- Imports:
Scannerfor user input.InputMismatchExceptionfor catching invalid inputs.
- Initialization:
maxNumPeopleis set to 10 (maximum capacity).totalNumPeopletracks the current number of people in the restaurant.
- Loop Condition:
- The
whileloop runs untiltotalNumPeopleis greater than or equal tomaxNumPeople.
- The
- Input Handling:
- Inside the loop, user input is read with
scnr.nextInt(). - If input is invalid (non-integer), an
InputMismatchExceptionis caught, and the program informs the user with an error message and discards the invalid input usingscnr.next().
- Inside the loop, user input is read with
- Bounds Checking:
- If
totalNumPeoplebecomes negative due to people leaving, it is reset to 0 to ensure occupancy can’t be less than zero.
- If
- Output:
- After every valid input, the current occupancy is printed.
- When the loop exits (occupancy reaches or exceeds 10), a message “We’re full!” is displayed.
Example Run:
Input:
2
abc
3
-1
8
Output:
Enter the number of people entering or leaving:
Occupancy: 2
Error: Invalid input. Please enter an integer.
Enter the number of people entering or leaving:
Occupancy: 5
Enter the number of people entering or leaving:
Occupancy: 4
Enter the number of people entering or leaving:
Occupancy: 12
We're full!
Let me know if you need further clarification or assistance!