Question: Write a Java application called GradedExercise2 that fulfills this specification. You may be able to reuse some of the code for GradedExercise1 Ask the user

Write a Java application called GradedExercise2 that fulfills this specification. You may be able to reuse some of the code for GradedExercise1 Ask the user for their age. If their age is under 21, or over 100, the program throws an exception.

And the program ends.

If the users age is between 21 and 100, the program outputs:

Your age is: (whatever the user has entered).

And the program ends.

If the user enters a number which is less than 21, the program outputs the following, and continues to accept input from the user until the user enters an age between 21 and 100.

java.util.InputMismatchException: You must enter a valid age.

That age is too low. Please try again.

Please enter your age:

If the user enters a number which is greater than 21, the program outputs the following, and continues to accept input from the user until the user enters an age between 21 and 100.

java.util.InputMismatchException: You must enter a valid age.

That age is too high. Please try again.

Please enter your age:

Suggestion: Declare a variable to store the users age in main(), and before the loop, or the try. This is because the variable needs to be visible from all trys and catches.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!