Discover a wealth of knowledge at Westonci.ca, where experts provide answers to your most pressing questions. Discover comprehensive solutions to your questions from a wide network of experts on our user-friendly platform. Our platform offers a seamless experience for finding reliable answers from a network of knowledgeable professionals.
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
We hope our answers were useful. Return anytime for more information and answers to any other questions you have. We appreciate your time. Please revisit us for more reliable answers to any questions you may have. We're dedicated to helping you find the answers you need at Westonci.ca. Don't hesitate to return for more.