Question: The while loop makes up to three attempts to read an integer divisible by 4 from input into numTires. Write a catch block to catch
The while loop makes up to three attempts to read an integer divisible by from input into numTires.
Write a catch block to catch any InputMismatchException thrown. In the catch block:
Output "Unexpected input: The TotalTires program exits".
Assign triesLeft with
Then, write another catch block to catch any Exception thrown. In the catch block:
Output the message of the Exception.
Subtract from triesLeft.
End each output with a newline.
Click here for examples
Ex : If the input is then the output is:
Tries left: Valid input: tires can make automobiles
Ex : If the input is P then the output is:
Tries left:
Unexpected input: The TotalTires program exits
Ex : If the input is then the output is:
Tries left:
The number of tires must be divisible by
Tries left:
Valid input: tires can make automobiles
import java.util.Scanner;
import java.util.InputMismatchException;
public class TotalTires
public static void mainString args
Scanner scnr new ScannerSystemin;
int numTires;
int triesLeft ;
while triesLeft
System.out.printlnTries left: triesLeft;
try
numTires scnrnextInt;
if numTires
throw new ExceptionThe number of tires must be divisible by ;
triesLeft ;
System.out.printValid input: ;
System.out.printlnnumTires tires can make numTires automobiles";
Your code goes here
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
