← Back to User Input
Question 42
Character Option Validator
Character Option Validator
Create an inputCharOption() method that accepts a Scanner and a String of valid characters. It should: 1. Read input using nextLine() 2. Check if input is exactly 1 character AND is in the validChars string 3. If valid, return the character 4. If invalid, print "ERROR: Character must be one of [validChars]: " and try again In main(), use it to get Y or N, then display: You entered: [character] Example: If user enters "a", "n", then "N", output shows errors then: You entered: N ⚠️ IMPORTANT - AUTO-INPUT MODE: This question uses automated input testing. When you run your code, the input stream will AUTOMATICALLY contain: 'N\n' DO NOT wait for manual keyboard input - Scanner will read from the pre-filled buffer stream immediately. Your validation loop should use scanner.nextLine() to read input. Think of it like Eclipse/VS Code but with the input already typed in and waiting for your Scanner to read it.
Expected Output:
You entered: N
Topics:
User Input
Code Editor
1
Tab to indent · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (sysout, psvm, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.