In: Computer Science
•Write a JAVA program to check a given password strength from a user's input.
•Create a method to check the number of characters. It must be more than 8.
•Create a method to check the password to have at least one uppercase letter.
•Create a method to check the password to have at least one lowercase letter.
•Create a method to check the password to have at least one digit.
•Create a method to check the password to have at least one special character (!, 2, #, %, &, *, (, ), etc.).
import java.util.Scanner;
public class PasswordValidate {
public static boolean hasLength(String password) {
return password.length() > 8;
}
public static boolean hasUppercase(String password) {
for (int i = 0; i < password.length(); i++) {
if (Character.isUpperCase(password.charAt(i)))
return true;
}
return false;
}
public static boolean hasLowercase(String password) {
for (int i = 0; i < password.length(); i++) {
if (Character.isLowerCase(password.charAt(i)))
return true;
}
return false;
}
public static boolean hasDigit(String password) {
for (int i = 0; i < password.length(); i++) {
if (Character.isDigit(password.charAt(i)))
return true;
}
return false;
}
public static boolean hasSpecial(String password) {
for (int i = 0; i < password.length(); i++) {
if ("!@#%$%^&*()".contains("" + password.charAt(i)))
return true;
}
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter password: ");
String password = in.next();
if (hasLength(password) && hasLowercase(password) && hasUppercase(password) && hasDigit(password) && hasSpecial(password)) {
System.out.println(password + " is valid");
} else {
System.out.println(password + " is invalid");
}
}
}