Loading...
Loading...
A sign-up form keeps asking for the user's age until it gets a good one. A good age is a whole number from 1 to 120.
Each input line is one attempt. For each bad attempt print why it was rejected: Not a number: <text> or Out of range: <age>. On the first good attempt print Age accepted: <age> and stop reading. If the lines run out before a good age, print No valid age entered.
No valid age entered if no attempt was good.Example 1
Input
abc -5 200 30
Output
Not a number: abc Out of range: -5 Out of range: 200 Age accepted: 30
Three bad attempts, then a good one.
Example 2
Input
x y
Output
Not a number: x Not a number: y No valid age entered
The lines ran out.
Try the problem first. Open one hint at a time.
Loop while no age is accepted and sc.hasNextLine() is true.
Read each attempt with nextLine() and convert it with Integer.parseInt inside a try.
A number can parse fine and still be wrong: check the range with an if after parsing.
Use a boolean accepted flag to stop the loop and to decide whether to print the final message.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor