Java Reference
In-Depth Information
16. String verifyprompt = “Verify password:”;
17.
18. String username = console.readLine(“%18s “, userprompt);
19. char [] password = console.readPassword(“%18s “, passprompt);
20. char [] verify = console.readPassword(“%18s “, verifyprompt);
21. if(password == null || !Arrays.equals(password, verify)) {
22. System.out.println(“Passwords do not match”);
23. return;
24. }
25. if(!Arrays.equals(password, “qwerty123”.toCharArray())) {
26. System.out.println(“Invalid login”);
27. return;
28. }
29. System.out.println(“Login successful!”);
30. //Remove password from memory
31. for(int i = 0; i < password.length; i++) {
32. password[i] = 'x';
33. verify[i] = 'x';
34. }
The following breakdown illustrates what the code does:
1. Line 10 obtains a reference to the Console object and lines 11-13 verify that the Con-
sole is available.
2. Line 18 displays ”Enter username:” formatted as 18 characters and the program
waits for the user to input a line of text at the console.
3. Lines 19 and 20 prompt the user to input a password twice. The program waits for
input each time and users cannot see their input.
4. Line 21 uses the Arrays.equals method for comparing two arrays of type char . If
password is null or the two input passwords are different, the method returns.
5. Line 25 compares the password input by the user to ”qwerty123” .
6. The for loop on line 31 overwrites the two passwords input by the user with x s (this is
done for security purposes).
The output varies depending on what the user inputs. The following output occurs if the
user inputs any username and ”qwerty123” for both passwords:
Enter username: raposa
Password:
Verify password:
Login successful!
The output does not show the passwords because the readPassword method disables the
echoing of user input.
Search WWH ::




Custom Search