019-001-005
Basic Exception Handling: Number Conversion
Hard
Problem Description
Basic exception handling: Number Conversion
Learning Objective: Use try-catch to catch and handle exceptions appropriately
In this problem, you will create a program that reads a string from standard input, converts it to an integer using Integer.parseInt(), catches NumberFormatException with a try-catch block, and displays the result on success or an error message on failure to standard output.
Input
Line 1: String to convert
Output
- For valid number: "Result: [number]"
- If conversion fails: "Error: Invalid number format"
Examples
Example 1 (Success)
Input:
42
Output:
Result: 42
Example 2 (Error)
Input:
abc
Output:
Error: Invalid number format
Example 3 (Success)
Input:
-100
Output:
Result: -100
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