Create a program [YourName]-Assignment8(replace [YourName] with your actual name
ID: 3757593 • Letter: C
Question
Create a program [YourName]-Assignment8(replace [YourName] with your actual name) that requires users to enter their own password and validates it. The password has to meet the following criteria:
1) The password should be at least eight characters long
2) The password should contain at least one uppercase letter
3) The password should contain at least one lowercase letter
4) The password should have at least one digit
5) The password should have at least one of the following symbols: #, $, &, *, ^, +, -, or @.
The program should allow the user to enter a password and then display a message indicating it is valid or not. If the password is not correct the program should ask the user again for a password and validate it repeatedly, until a valid/correct password is provided.
Explanation / Answer
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SrinivasAssignment8 {
/*n!k@s // it's less that 8 characters long
gregorymarjames-law // it doesn't contain an digits or upper case characters
abcdFg45| // characters | in not allowed
n!koabcD#AX // there should be a digit
ABCASWF2! // there should be a lower case character
n!k@sn1Kos //VALID
*/
public static void main(String args[]) {
boolean flag = true;
do {
System.out.print("Enter Password to Validate :");
Scanner scanner = new Scanner(System.in);
String password = scanner.nextLine();
if (validate(password) == true) {
flag = false;
System.out.println("Password is matched");
} else {
System.out.println("Password doesnt match Try again!");
}
} while (flag);
}
static Pattern pattern;
static Matcher matcher;
static final String
PASSWORD_PATTERN ="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%&*^+-]).{8,20})";
public static boolean validate(final String password) {
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();
}
}
OUTPUT:
Enter Password to Validate :*n!k@s
Password doesnt match Try again!
Enter Password to Validate :gregorymarjames-law
Password doesnt match Try again!
Enter Password to Validate :abcdFg45|
Password doesnt match Try again!
Enter Password to Validate :n!koabcD#AX
Password doesnt match Try again!
Enter Password to Validate :ABCASWF2!
Password doesnt match Try again!
Enter Password to Validate :n!k@sn1Kos
Password is matched
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.