Westonci.ca connects you with experts who provide insightful answers to your questions. Join us today and start learning! Our platform provides a seamless experience for finding reliable answers from a network of experienced professionals. Get precise and detailed answers to your questions from a knowledgeable community of experts on our Q&A platform.
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
Thank you for visiting our platform. We hope you found the answers you were looking for. Come back anytime you need more information. We appreciate your time. Please come back anytime for the latest information and answers to your questions. Thank you for visiting Westonci.ca, your go-to source for reliable answers. Come back soon for more expert insights.