{"id":184868,"date":"2025-01-21T17:55:08","date_gmt":"2025-01-21T17:55:08","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=184868"},"modified":"2025-01-21T17:55:10","modified_gmt":"2025-01-21T17:55:10","slug":"i-tried-this-many-times-and-still-cannot-get-through-it","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/01\/21\/i-tried-this-many-times-and-still-cannot-get-through-it\/","title":{"rendered":"I tried this many times and still cannot get through it"},"content":{"rendered":"\n<p>I tried this many times and still cannot get through it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/01\/image-370-1024x567.png\" alt=\"\" class=\"wp-image-184869\"\/><\/figure>\n\n\n\n<p>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 &#8220;2 abc 8&#8221; 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(&#8220;Error&#8221;); } if (totalNumPeople &lt; 0)=&#8221;&#8221; {=&#8221;&#8221; totalnumpeople=&#8221;0;&#8221; }=&#8221;&#8221; catch=&#8221;&#8221; (inputmismatchexception=&#8221;&#8221; e)=&#8221;&#8221; {=&#8221;&#8221; system.out.println(&#8220;occupancy:=&#8221;&#8221; &#8220;=&#8221;&#8221; +=&#8221;&#8221; totalnumpeople);=&#8221;&#8221; }=&#8221;&#8221; totalnumpeople=&#8221;&#8221; +=&#8221;&#8221; scnr.nextint=&#8221;&#8221; ();=&#8221;&#8221; catch=&#8221;&#8221; {=&#8221;&#8221; try=&#8221;&#8221; {=&#8221;&#8221; }=&#8221;&#8221; while=&#8221;&#8221; (totalnumpeople=&#8221;&#8221;>&lt; maxnumpeople)=&#8221;&#8221; {=&#8221;&#8221; maxoccupancy=&#8221;&#8221; tracker.java=&#8221;&#8221; e=&#8221;&#8221; zybooks=&#8221;&#8221; catalog=&#8221;&#8221; import=&#8221;&#8221; java.util.scanner;=&#8221;&#8221; import=&#8221;&#8221; java.util.=&#8221;&#8221; inputmismatchexception;=&#8221;&#8221; }=&#8221;&#8221; load=&#8221;&#8221; default=&#8221;&#8221; template&#8230;=&#8221;&#8221; public=&#8221;&#8221; class=&#8221;&#8221; maxoccupancytracker=&#8221;&#8221; {=&#8221;&#8221; public=&#8221;&#8221; static=&#8221;&#8221; void=&#8221;&#8221; main=&#8221;&#8221; (string[]=&#8221;&#8221; args)=&#8221;&#8221; {=&#8221;&#8221; scanner=&#8221;&#8221; scnr=&#8221;new&#8221; scanner(system.in);=&#8221;&#8221; int=&#8221;&#8221; maxnumpeople=&#8221;10;&#8221; int=&#8221;&#8221; totalnumpeople=&#8221;0;&#8221; check=&#8221;&#8221; x=&#8221;&#8221; failed=&#8221;&#8221; to=&#8221;&#8221; compile=&#8221;&#8221; maxoccupancytracker=&#8221;&#8221; java:17:=&#8221;&#8221; error:=&#8221;&#8221; &#8216;catch&#8217;=&#8221;&#8221; without=&#8221;&#8221; &#8216;try!=&#8221;&#8221; system.out.println(&#8220;we&#8217;re=&#8221;&#8221; full!&#8221;);=&#8221;&#8221; help\/faq=&#8221;&#8221; sydney=&#8221;&#8221;><\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\">The correct answer and explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>To create a functioning Java program for the described &#8220;restaurant max occupancy tracker&#8221; using exception handling, you need to properly organize the given code fragments. Below is the complete and corrected code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\nimport java.util.InputMismatchException;\n\npublic class MaxOccupancyTracker {\n    public static void main(String&#91;] args) {\n        Scanner scnr = new Scanner(System.in);\n        int maxNumPeople = 10;\n        int totalNumPeople = 0;\n\n        while (totalNumPeople &lt; maxNumPeople) {\n            try {\n                System.out.println(\"Enter the number of people entering or leaving:\");\n                totalNumPeople += scnr.nextInt();\n\n                if (totalNumPeople &lt; 0) {\n                    totalNumPeople = 0; \/\/ Prevent negative occupancy\n                }\n\n                System.out.println(\"Occupancy: \" + totalNumPeople);\n            } catch (InputMismatchException e) {\n                System.out.println(\"Error: Invalid input. Please enter an integer.\");\n                scnr.next(); \/\/ Discard invalid input\n            }\n        }\n\n        System.out.println(\"We're full!\");\n        scnr.close();\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Imports:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>Scanner<\/code> for user input.<\/li>\n\n\n\n<li><code>InputMismatchException<\/code> for catching invalid inputs.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Initialization:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>maxNumPeople<\/code> is set to 10 (maximum capacity).<\/li>\n\n\n\n<li><code>totalNumPeople<\/code> tracks the current number of people in the restaurant.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Loop Condition:<\/strong>\n<ul class=\"wp-block-list\">\n<li>The <code>while<\/code> loop runs until <code>totalNumPeople<\/code> is greater than or equal to <code>maxNumPeople<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Input Handling:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Inside the loop, user input is read with <code>scnr.nextInt()<\/code>.<\/li>\n\n\n\n<li>If input is invalid (non-integer), an <code>InputMismatchException<\/code> is caught, and the program informs the user with an error message and discards the invalid input using <code>scnr.next()<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Bounds Checking:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If <code>totalNumPeople<\/code> becomes negative due to people leaving, it is reset to 0 to ensure occupancy can&#8217;t be less than zero.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Output:<\/strong>\n<ul class=\"wp-block-list\">\n<li>After every valid input, the current occupancy is printed.<\/li>\n\n\n\n<li>When the loop exits (occupancy reaches or exceeds 10), a message &#8220;We&#8217;re full!&#8221; is displayed.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example Run:<\/h3>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2\nabc\n3\n-1\n8\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the number of people entering or leaving:\nOccupancy: 2\nError: Invalid input. Please enter an integer.\nEnter the number of people entering or leaving:\nOccupancy: 5\nEnter the number of people entering or leaving:\nOccupancy: 4\nEnter the number of people entering or leaving:\nOccupancy: 12\nWe're full!\n<\/code><\/pre>\n\n\n\n<p>Let me know if you need further clarification or assistance!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-184868","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/184868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=184868"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/184868\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=184868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=184868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=184868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}