Input Validator - Problem
Create a program that continuously prompts the user for an integer input until a valid integer is provided. The program should:
- Keep asking for input in a loop
- Handle invalid input gracefully with error messages
- Return the first valid integer entered
- Use proper exception handling to catch conversion errors
Note: Your function will receive a list of input strings that simulate user input attempts. Process them in order until you find a valid integer.
Input & Output
Example 1 — Mixed Invalid and Valid Inputs
$
Input:
inputs = ["abc", "12x", "42"]
›
Output:
42
💡 Note:
Skip "abc" (not a number), skip "12x" (contains letter), return 42 (first valid integer)
Example 2 — Negative Numbers
$
Input:
inputs = ["-5.5", "-10", "hello"]
›
Output:
-10
💡 Note:
Skip "-5.5" (decimal), return -10 (first valid integer), never reach "hello"
Example 3 — First Input Valid
$
Input:
inputs = ["123", "invalid", "456"]
›
Output:
123
💡 Note:
Return 123 immediately as it's the first valid integer in the list
Constraints
- 1 ≤ inputs.length ≤ 100
- Each input string length ≤ 50 characters
- At least one valid integer will be present in the inputs
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code