loops for input validation

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Posts: 48
Joined: Mon Jun 25, 2012 9:39 pm UTC

loops for input validation

Postby Rosewinsall » Thu Oct 01, 2015 5:20 pm UTC

I am in a java programming course in uni and I am having a very hard time getting loops. Specifically my problems are with while loops and how to use them for input validation.

User avatar
Posts: 6862
Joined: Fri Sep 28, 2012 3:23 pm UTC
Location: The One True Thread

Re: loops for input validation

Postby ucim » Thu Oct 01, 2015 6:30 pm UTC

Is the problem conceptual (with loops themselves) or specifically with the java syntax for them? Can you give an example of something you don't understand?

In general, while loops are like (I'm using PHP syntax, java should be similar):

while ( something_is_true)
{ do_this_stuff;

It's like an if statement that keeps coming back for more.

For input validation, it would look like:
$input_bad = TRUE; // force the while condition to be true to ensure at least one pass...
while ($input_bad === TRUE)
{ $input_bad = is_input_bad(get_input());
// your validation function

So, before entry into the loop, a variable that represents input horribleness is set to TRUE (no input is bad input!)
The while condition is evaluated (it is TRUE because (no) input is horrible).
Because the condition is TRUE, the body executes.
{ inside the body, your get_input() function executes (presumably returning what was typed in). This is fed to your is_input_bad() function, which figures out how horrible the user's input is. If it is horrible (and we want the user to try again), the is_input_bad() function returns TRUE, otherwise it returns FALSE.

This is the end of the body. Now the while condition is evaluated again to see if we keep this up. So long as the user hasn't a clue (as evidenced by the return value of your is_input_bad() function), the body gives him or her another shot. But once the user is clueful, as demonstrated by their flawless typing in of whatever it is you want out of them, the is_input_bad() function will return FALSE. When evaluated in the while condition, this FALSE result will free your program from pounding on the user, and your program can then use (or abuse) the input as it sees fit.

There are a few other approaches whose syntax might be supported, such as
{ this_stuff;
} while (condition_is_true)

which implicitly forces the first pass into the loop, but the essence is the same.

Note: in my example (using PHP syntax), = is an assigment operator, and === is a comparison operator.
== is also a comparison operator, but fudges the data type. I don't know java but it's probably similar.

Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Heartfelt thanks from addams and from me - you really made a difference.

Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests