Write a method to prompt the user to choose a password. The password must contai
ID: 3678872 • Letter: W
Question
Write a method to prompt the user to choose a password. The password must contain at least one uppercase letter, one lowercase letter, one digit, and one special character (defined as a character that is neither a letter nor a digit). If the string the user enters does not meet these requirements, display an error message indicating the unmet requirement(s) and re-prompt the user. Below is a sample execution. output should look similar to this :
Please choose a password: mypassword
Your password must contain a uppercase letter
Your password must contain a digit
Your password must contain a special character
Please choose a password: myPassword
Your password must contain a digit
Your password must contain a special character
Please choose a password: my_Password
Your password must contain a digit
Please choose a password: my_Password1234
Password = my_Password1234
Explanation / Answer
public class createPassword { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter a given password : "); String passwordhere = in.nextLine(); System.out.print("Please re-enter the password to confirm : "); String confirmhere = in.nextLine(); while (!passwordhere.equals(confirmhere) || !isValid(passwordhere)) { System.out.println("The password entered here is invalid"); System.out.print("Please enter the password again.it must be valid : "); String Passwordhere = in.nextLine(); System.out.print("Please re-enter the password to confirm : "); String confirmhere = in.nextLine(); } } public static boolean isValid(String passwordhere) { if (passwordhere.length() < 8) { return false; } else { for (int p = 0; pRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.