Garbage in = garbage out.

If your user inputs unexpected values from the keyboard your program can crash or give the incorrect result. Defensive programming is the way that we avoid this.

Defensive programming is also super useful for login methods.

NOTE: Use defensive programming in your PAT but not in an exam unless it is asked for; the examiner can only give you the marks indicated in the memorandum.

Here is a (growing) list of defensive programming techniques.

1) While loop with NOT – String – Java

Reject all input until a valid value is entered. NOT in Java is “!” (exclamation mark) and is is used differently with Strings and integers. Note the double input lines  . . . one before the while loop and one inside.

String myPassword = “abcd”;

String myPasswordAttempt = JOptionPane.showInputDialog (“Enter your password”);

while(! myPasswordAttempt.equals(myPassword))
{
      myPasswordAttempt = JOptionPane.showInputDialog (“Enter your password”);
}

2) While loop with NOT – integer

Enter integer by whatever means . . .

while (myInteger ! = 5) {

      Enter integer by whatever means again . . 

}

This means that while the number is not equal to 5 the user must input the number again.

3) While loop with NOT and AND – String

String myPassword = “ABC_123_xyz”;
int counter = 0;
String myPasswordAttempt = JOptionPane.showInputDialog (“Enter your password”);

// This loop will loop until the password is correct or the maximum number of tries is reached
while(!myPasswordAttempt.equals(myPassword) && (counter <= 10))
{
        myPasswordAttempt = JOptionPane.showInputDialog (“Enter your password”);
        counter = counter + 1;
}

4) While loop with NOT and AND – String. Limiting the responses e.g. gender M or F

String gender = JOptionPane.showInputDialog(“Enter your gender.”);
String shortGender = gender.substring(0,1);
shortGender = shortGender.toUpperCase();

// This loop will loop until gender is either M or F

while (!shortGender.equals(“M”) && !shortGender.equals(“F”))

{
        gender = JOptionPane.showInputDialog(“Enter your gender”);
        shortGender = gender.substring(0,1);
        shortGender = shortGender.toUpperCase();
}

5) While loop with NOT and AND – char. Limiting the responses e.g. gender M or F

String gender = JOptionPane.showInputDialog(“Enter your gender.”);
gender = gender.toUpperCase();
char shortGender = gender.charAt(0);

// This loop will loop until gender is either M or F

while ((shortGender != ‘M’) && (shortGender != ‘F’))
{
       gender = JOptionPane.showInputDialog(“Enter your gender”);
       gender = gender.toUpperCase();
       shortGender = gender.charAt(0);
}

6) While loop that does not allow 2 random numbers to be the same

int myRandom1 = 0, myRandom2 = 0;
while(myRandom1 == myRandom2) {
      myRandom1 = (int)(Math.random() * (20 + 1) + 1);
      myRandom2 = (int)(Math.random() * (20 + 1) + 1);
}

While the two random numbers are the same, generate them again. Needs to start with both numbers being set to zero so that the loop starts. In this case the random numbers are from 1 to 20 inclusive.