subject
Computers and Technology, 07.08.2021 01:30 7841784

The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer.
At FIXME in the code, add a try/catch statement to catch java. util. InputMismatchException, and output 0 for the age.

Ex: If the input is:

Lee 18
Lua 21
Mary Beth 19
Stu 33
-1
then the output is:

Lee 19
Lua 22
Mary 0
Stu 34

import java. util. Scanner;
import java. util. InputMismatchException;

public class NameAgeChecker {
public static void main(String[] args) {
Scanner scnr = new Scanner(System. in);

String inputName;
int age;
inputName = scnr. next();
while (!inputName. equals("-1")) {
// FIXME: The following line will throw an InputMismatchException.
// Insert a try/catch statement to catch the exception.
age = scnr. nextInt();
System. out. println(inputName + " " + (age + 1));
inputName = scnr. next();
}
}
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 11:00
Which action is good business etiquette? a. switching your cell phone off before you enter a meeting b. keeping your cell phone on low volume before you enter a meeting c. setting a pleasant ring tone on your cell phone before you enter a meeting d. setting a standard ringtone on your cell phone before you enter a meeting
Answers: 1
question
Computers and Technology, 22.06.2019 16:00
Why should characters such as / \ " ' * ; - ? [ ] ( ) ~ ! $ { } < > # @ & | space, tab, and newline be avoided in file names?
Answers: 2
question
Computers and Technology, 23.06.2019 16:30
How to do this programming flowchart?
Answers: 3
question
Computers and Technology, 24.06.2019 01:00
The initial tableau of a linear programming problem is given. use the simplex method to solve it. x 1 x 2 x 3 s 1 s 2 z 1 2 4 1 0 0 8 3 4 1 0 1 0 10 minus3 minus12 1 0 0 1 0 the maximum is nothing when x 1equals nothing, x 2equals nothing, x 3equals nothing, s 1equals3, and s 2equals0. (be sure to simplify to lowest terms if necessary.)
Answers: 2
You know the right answer?
The given program reads a list of single-word first names and ages (ending with -1), and outputs tha...
Questions
question
Biology, 04.05.2021 03:20
question
Biology, 04.05.2021 03:20
question
Mathematics, 04.05.2021 03:20
question
English, 04.05.2021 03:20
question
Mathematics, 04.05.2021 03:20
Questions on the website: 13722367