002-003-011
Exception Handling in Input Parsing
Hard
Problem Description
Exception Handling in Input Parsing
In this problem, you will create a program that implements exception handling (using try-catch and NumberFormatException) when converting strings to integers, and displays success or failure messages to standard output.
Learning Objective: Understand how to properly handle exceptions when parsing numbers
Overview
Read strings with Scanner, convert each to an integer, and display appropriate error messages for invalid input.
Input Format
N
str1
str2
...
strN
- Line 1: number of strings to process N (1 ≤ N ≤ 100)
- Subsequent lines: each string
Specifications
parseNumber(String input): converts string to integer, returns -1 on failure- On success: output "Parsed: number"
- On failure: output "Invalid number: input"
- In
main, read input from Scanner and process each string
Output Format
Output one result line per input string.
Sample Input
3
42
abc
100
Sample Output
Parsed: 42
Invalid number: abc
Parsed: 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