019-001-011
Exception Handling: Number Format Error
Easy
Problem Description
Exception Handling: Number Format Error
In this problem, you will implement a static method parseNumber(String input) that converts a string to an integer. It should return the integer value when conversion is possible, and throw NumberFormatException when the string cannot be converted.
Learning Objective: Understand the conditions under which Integer.parseInt() throws NumberFormatException and how exceptions propagate
Specifications
Implement a static method with the following signature:
public static int parseNumber(String input)
- If
inputcan be interpreted as an integer, return itsintvalue - If
inputcannot be interpreted as an integer, throwNumberFormatException(achieved naturally by delegating toInteger.parseInt())
Convertible Examples
"123"→ 123"-456"→ -456"0"→ 0
Non-convertible Examples (NumberFormatException)
"abc"→ exception"12.5"→ exception (strings with decimal points are not allowed)""→ exception (empty string is not allowed)
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up