PAT - Verifying User Input (Lesson)

 User Input Control

The use of a while loop to control input to the program, whether user input or other input, is important to prevent a major program error from input causing erroneous conditions in a program.  If you wanted only the numbers 1 through 5 to be used as options, are you checking what happens if the user enters a 6 or a 100?  If not, depending on your code, the 100 could act the same as a 6 or cause other unknown errors when the input is in use.

User input can be controlled with a while loop during entry into the program.  Below are two examples of a while loop verifying the input from the user and returning a message to the user in case of an error.  The loop runs until the information entered is correct. 

 

Notes for Option 1 below:

  • Here the while loop is used to check if the current selection is outside the range of numbers required.  
  • If the number entered by the user is outside the range, either less than 1 or greater than 5, then the user is told incorrect and asked again for the correct range of a number between 1 and 5 inclusive.
  • If the original number entered is correct, the while loop is bypassed as the correct whole number 1, 2, 3, 4, 5 has been entered and can continue the steps of the program.
  • Option 1 used 2 user input statements.

Check option 1 for GUI input.png

 

Notes for Option 2 below:

  • This option sets the initial input number of our range, so that at least one entry into the while loop must happen.
  • Upon entry into the while loop an if - else statement controls the message the user gets, depending on whether the variable nbr1 is zero or another number, allowing for two messages.  The zero message is appropriate for having not entered anything or a value of zero from the user input.
  • Just prior to the while completion, the user is asked for input.  This input is checked as the while loop goes back to the top of its loop with an appropriate message if incorrect.
  • If the user input is correct, an integer 1 to 5 inclusive, then the while loop is ended and further code statements are done.
  • The variable cntTries is set here in case the number of tries was to be set to a specific number and the loop exits.
  • Option 2 used only one input statement.

Check option 2 for GUI input.png

 

What about text input?  Can this be checked?  Yes, but a little differently.  

Notes for Option 3 below:

  • This option used a textString control for the while loop, but a boolean could be used as well.
  • The use of textString control contains (there are others) is used here to check for when the while loop will exit.
  • The message inside the loop is generic for all entries, but a first time through variable could allow you to tell if the input was original or user input.
  • The check variable beta is changed to okay to force the exit from the loop with a valid answer.
  • An option of equalsIgnoreCase is available if capitals would be accepted.
  • Option 3 used one input statement.

Check option 3 alpha.png

 

IMAGE CREATED BY GAVS AND USED ACCORDING TO TERMS OF USE.