CJCoding With Joseph
15free / daySign inCreate account

Write a program with an inputInt() method that safely reads an integer with validation. The method should: 1. Read input as a string using nextLine() 2. Try to convert it to an integer using Integer.parseInt() 3. If conversion fails, print "Error! Input a whole number: " and try again 4. Return the valid integer

In main(), call inputInt() and display:

You entered: [number]

Example: If user enters "a", "a1", "1.1", then "1", output should show errors for first 3 inputs then prints the output shown in Expected Output.

⚠️ IMPORTANT - AUTO-INPUT MODE: This question uses automated input testing. When you run your code, the input stream will AUTOMATICALLY contain: '1\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: 1

= newline. The output ends with a newline, so print one after the last line.

Hints4 available
Topics
User Input
Auto-input
1\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · 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.