Discussion of Activity 2.3 - Using if statements

An example solution is shown below. The first condition checks to see if the user ID the user entered from the keyboard, stored in the variable id is the same as the value stored in the variable userID. If it is, we can check the entered code to see if it matches the stored registrationCode. This second condition is tested by a nested if.

We have taken the view that there is no need to check the registration code if the user ID was incorrect, as the registration cannot be accepted.

We have not shown code to allow the user to re-enter their details, as we have not yet covered the programming structures you require to do this.

if (id == userID)
{
   if (code == registrationCode)
   {
      //Both correct!
      System.out.println("Your registration is accepted.");
   }
   else
   {
      //The id was correct but then the code was wrong.
      System.out.println("Sorry, your registration code was incorrect. Please try  again.");
   }
}
else
{
   //The id was wrong.
   System.out.println("Sorry, your userID was incorrect. Please try again.");
}