Discover answers to your questions with Westonci.ca, the leading Q&A platform that connects you with knowledgeable experts. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform.

Exception handling to detect input String vs. Integer 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 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 LAB ACTIVITY 11.6.1: LAB: Exception handling to detect input String vs. Integer 0 / 10 Name AgeChecker.java Load default template.. 5 int age: 1 import java.util.Scanner; 2 import java.util. Input MismatchException; 3 4 public class Name AgeChecker public static void main(String[] args) { 6 Scanner sehr new Scanner(System.in); 7 8 String inputName; 9 10 11 input.Name = sehr.next(); 12 while (!inputName.equals("-1")) { 13 // FIXME: The following line will throw an InputMismatchException. 14 Insert a try/catch statement to catch the exception. 15 age = sehr.nextInt(): 16 System.out.println(input.Name + " (age + 1)); 17 18 inputName = scnr.next(); 19 20 21 }

Sagot :

Following are the program to the given question:

import java.util.Scanner;//import package

import java.util.InputMismatchException;//import package

public class NameAgeChecker //defining a class NameAgeChecker

{

   public static void main(String[] axc)//defining main method

   {

       String inputName;//defining string variable

       int age;//defining integer variable

       Scanner sehr = new Scanner(System.in);//creating Scanner class object

       inputName = sehr.next();//input string value

       while (!(inputName.equals("-1"))) //use while loop that check string value

       {

           try//defining a try block

           {

               age = sehr.nextInt();//use integer variable that input value

           }

           catch (InputMismatchException e)//defining catch block  

           {

               age = -1;//use age variable that hold negative value

               sehr.nextLine();//input value

           }

           System.out.println(inputName + " " + (age + 1));//print value

           inputName = sehr.next();//input string value

       }

   }

}

Output:

please find the output in the attached file.

Explanation:

  • Importing the packages.
  • Defining the main class "NameAgeChecker".
  • Inside the class define the main method, and the method one string and one integer variable "inputName, age" is declared.
  • Use a while loop that takes value from user-input from the user until the user enters -1.
  • In addition to printing the name and the incremented age, it will also print the name and the input's age.
  • If the second phase of the line is entered as a string, the software will print age as 0.

Learn more:

Program: brainly.com/question/2266606

View image codiepienagoya