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.
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 appreciate your time on our site. Don't hesitate to return whenever you have more questions or need further clarification. Thanks for using our platform. We aim to provide accurate and up-to-date answers to all your queries. Come back soon. Get the answers you need at Westonci.ca. Stay informed with our latest expert advice.