Skip to content
Exercise · Exceptions · lesson 12 of 12

Exercise: age check

Write a method static int parseAge(String s) that turns a string into an age. If the string is not a number, or the number is below 0 or above 150, the method throws IllegalArgumentException.

  • parseAge("30") → 30
  • parseAge("-1") → throws IllegalArgumentException
  • parseAge("abc") → throws IllegalArgumentException

Constraints: 0 and 150 are valid ages; an empty string is not a number.

Do not add extra imports unless the task says so explicitly.

Main.java — java
java · 6 lines
Ctrl+Enter run · Esc leave editor