// A simple but very insecure login system in which // everyone has the same password. // // DEVELOPMENT NOTES: // (These notes would not be in your program's comments. They are here // to help you understand important topics or elements of this code.) // // Notice the use of the String .equals method to check for equality, the use of // the ! (not) operator to negate the condition, and the use of a while loop to // continue prompting for a password until it is correct. // // We *must* use the .equals() method here to compare Strings to see if they are // equivalent (contain the same characters in precisely the same order). In // general, when testing objects for equivalence, we should use the .equals() // method not the == operator (which works as expected for *primitive* types). // // This while loop is an example of a fencepost loop because we are alternating // two tasks (prompting for a password and indicating the password is incorrect), // but will perform one of the tasks once more than the other. In this case, // we consider a small amount of redundancy acceptable. import java.util.*; public class LoginSystem { public static final String REAL_PASSWORD = "CSE142_Rocks!"; public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Enter your full name: "); String name = console.nextLine(); System.out.println("Welcome, " + name + "!"); System.out.print("Enter your password: "); String password = console.nextLine(); while (!password.equalsIgnoreCase(REAL_PASSWORD)) { System.out.println("Incorrect passwordl! Try again..."); System.out.print("Enter your password: "); password = console.nextLine(); } System.out.println("Access granted."); } }