Question: 1 2 . 1 2 LAB: Input errors with zyLabs Write a program that takes in three integers as inputs and outputs the largest value.

12.12 LAB: Input errors with zyLabs
Write a program that takes in three integers as inputs and outputs the largest value. Use a try block to perform all the statements. Use a catch block to catch any NoSuchElementException caused by missing inputs. Then output the number of inputs read and the largest value, or output "No max" if no inputs are read.
Note: Because inputs are pre-entered when running a program in the zyLabs environment, the system throws the NoSuchElementException when inputs are missing. Test the program by running the program in the Develop mode.
Hint: Use a counter to keep track of the number of inputs read and compare the inputs accordingly in the catch block when an exception is caught.
Ex: If the input is:
375
the output is:
7
Ex: If the input is:
3
the system throws the NoSuchElementException and outputs:
1 input(s) read:
Max is 3
Ex: If no inputs are entered:
the system throws the NoSuchElementException and outputs:
0 input(s) read:
No max
my code
import java.util.Scanner;
import java.util.NoSuchElementException;
public class LabProgram {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
int val1=0, val2=0, val3=0;
int max =0;
int inputCounter =0;
try {
String inputLine = scnr.nextLine();
String[] parts = inputLine.split("");
for (String part : parts){
if (!part.isEmpty()){
int value = Integer.parseInt(part);
inputCounter++;
if (inputCounter ==1){
val1= value;
max = value;
} else if (inputCounter ==2){
val2= value;
} else if (inputCounter ==3){
val3= value;
} else {
// Ignore extra inputs
}
if (value > max){
max = value;
}
}
}
} catch (NoSuchElementException ex){
System.out.println("0 input(s) read:
No max");
} catch (NumberFormatException ex){
System.out.println("Invalid input. Please provide integers.");
}
if (inputCounter >0){
System.out.println(max);
}
}
}
Latest submission -3:05 AM CDT on 04/30/24
Only show failing tests
1:Compare output
Input
Your output
2:Compare output
Output differs. See highlights below.
Special character legend
Input
Your output
Expected output 5:Compare output ???
Output differs. See highlights below.
Special character legend
Input 37
Your output
Expected output
2 input(s) read:
Max is 7
6:Compare output
Output differs. See highlights below.
Special character legend
Input
Your output

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 Programming Questions!