In: Computer Science
You are developing a software package for an online shopping
site that requires users to enter their own passwords. Your
software requires that users' passwords meet the following
criteria:
● The password should be at least six characters
long.
● The password should contain at least one uppercase and at
least one
lowercase letter.
● The password should have at least one digit.
Write a method that verifies that a password meets the stated
criteria. Use this method in a program that allows the user to
enter a password and then determines whether or not it is a valid
password. If it is valid, have the program print "Valid
Password". Otherwise, it should print "Invalid
Password".
Sample Run
java PasswordVerifier
Enter·password·to·be·verified:ComputerScience4Life↵
Valid·password↵
import java.util.Scanner;
import java.io.IOException;
import java.util.regex.Pattern;
// Java program to validate an password in Java
class PasswordVerifier
{
// minimum 6 characters password with at least one
digit, at least one
// lowercase letter at least one uppercase letter,
private static final String PASSWORD_REGEX =
"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=\\S+$).{6,}$";
private static final Pattern PASSWORD_PATTERN
=
Pattern.compile(PASSWORD_REGEX);
public static void main(String[] args)
{
Scanner myObj = new Scanner(System.in); // Create a
Scanner object
System.out.println("Enter·password·to·be·verified:");
String password = myObj.nextLine(); // Read user input
PasswordVerify(password);
}
// Validate an password
static void PasswordVerify(String password1) {
System.out.println(password1);
if (PASSWORD_PATTERN.matcher(password1).matches()) {
System.out.print("The Password " + password1 + " is valid");
}
else {
System.out.print("The Password " + password1 + " isn't
valid");
}
}
}