ID and Password Check

  Download Source Code

/*** Password ID Check ********************
This program inputs a user name and password
and checks whether these match each other.
It uses pop-up INPUT and OUTPUT windows. *******************************************/

Multiple Decisions

This program shows how to use if...else if ... else if ... else.. to choose exactly one action from a list of many choices.
Each else if... is only tested when the previous if.. is not true.  Whenever one of the checks succeeds, the rest of the
choices are not tested.

   if( name.equals("Admin") && pw.equals("master") )
{ output("Hello master"); }
The if.. command checks whether the user name is correct (Admin) AND the password is correct (master).
If these are correct, it outputs "Hello master".  Otherwise it goes on and checks the next combination:
   else if ( name.equals("RONALD") && pw.equals("clown") )
   { output("Ha..ha..hallo"); }
If these are correct, it prints "Ha..ha..hallo".  Otherwise, it goes on to check the next combination:
   else if ( name.equals("secret") && pw.equals("magic") )
   { output("Okay"); }
If this still doesn't work, then it goes on to the default result:
   { output("Rejected! Who are you?"); }
So if none of the combinations match the input, it prints "Rejected..." for any other input.
That is the DEFAULT result - that is the response for ANY other combination.

We use if .. else if .. else if .. else .. when we want EXACTLY ONE action to be chosen from many possibilities.


Using String.equals to compare strings is case-sensitive.  That means that CAPITAL letters and small letters must all
match exactly.  This is a normal procedure for inputting user-names and passwords.

If you prefer to do a NON-case-sensitive comparison, use .equalsIgnoreCase( ) instead of just .equals( ).

    if( name.equalsIgnoreCase("Admin") && pw.equalsIgnoreCase("master") )
{ output("Hello master"); }
This would accept "admin" and "master" typed with any combination of capital and small letters:

           Admin , Master    or   ADMIN , MASTER   or  ADMIN , masTER , etc.