Password Checking Example

The password checking example is a variant of indeterminate while loop:


public class PasswordCheckingExample {

    public static void main(String[] args) {
       
        String password="abc";
        boolean matched = false;
       
        while(matched == false) {
            String userPassword = Helper.readString("Enter password: ");
            if(userPassword.equals(password)) { //why is it wrong to use "=="?
                System.out.println("Correct!");
                matched = true;
            }
            else {
                System.out.println("Wrong! Please try again.");
            }
        }
        System.out.println("End of program");

    }

}
 
Last modified: Thursday, 17 January 2019, 10:49 AM