Question: The while loop makes multiple attempts to read a positive integer from input into cylinderLength. Use multiple exception handlers to: Catch an InputMismatchException, output Fatal

The while loop makes multiple attempts to read a positive integer from input into cylinderLength. Use multiple exception handlers to:
Catch an InputMismatchException, output "Fatal error: The CylinderLength program terminates", and assign tryAgain with false.
Catch an Exception and output the message of the Exception. import java.util.Scanner;
import java.util.InputMismatchException;
public class CylinderLength {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
int cylinderLength;
boolean tryAgain = true;
while (tryAgain){
try {
cylinderLength = scnr.nextInt();
if (cylinderLength <=0){
throw new Exception("Cylinder's length (in cm) must be positive");
}
tryAgain = false;
System.out.print("Valid input: ");
System.out.println("Cylinder's length (in cm) is "+ cylinderLength);
}
/* Your code goes here */
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!