Some websites impose certain rules for passwords Suppose pas
Some websites impose certain rules for passwords. Suppose password rules are as follows:
 1. A password must have at least eight characters.
 2. A password consists of only letters and digits.
 3. A password must contain at least two digits.
 Write a program with class name CheckPassword that prompts the user to enter a password and displays Valid password if the rules are followed or Invalid password otherwise. Create three methods (headers given below) to check the three rules.
Solution
Please follow the code and comments for description :
CODE :
import java.util.Scanner; // required imports for the code to run
 import java.util.regex.Pattern;
public class CheckPassword { // class to run the code
public static boolean eightChar(String password) { // method that checks if the length is a 8 or not
 if (password.length() < 8) { // check for the length
 return false;
 } else {
 return true;
 }
 }
public static boolean letterDigits(String password) { // method that checks if it has letter and digits or not
 final Pattern hasNumber = Pattern.compile(\"\\\\d\"); // pattern class used to check ofr the required patterns
 final Pattern hasLetters = Pattern.compile(\"[A-Za-z]\");
 if (hasNumber.matcher(password).find() && hasLetters.matcher(password).find()) { // check for the requirement
 return true;
 } else {
 return false;
 }
 }
public static boolean twoDigits(String password) { // method that checks if the string has atleast two digits or not
 int count = 0; // local variables
 for (int i = 0; i < password.length(); i++) { // iterating over the length of the string
 if (Character.isDigit(password.charAt(i))) { // check for the digits
 count++; // increment the count
 }
 }
 if (count >= 2) { // check the count
 return true;
 } else {
 return false;
 }
 }
public static void main(String[] args) { // driver method
Scanner sc = new Scanner(System.in); // scanner class that takes the input
 System.out.println(\"Please Enter the Password to Validate :\"); // prompt for the user to enter the data
 String pass = sc.nextLine(); // get the data
boolean lengthVerify = eightChar(pass); // method call that checks the condition
 boolean letterDigitsVerify = letterDigits(pass); // method call that checks the condition
 boolean digitsCountVerify = twoDigits(pass); // method call that checks the condition
if ((lengthVerify == true) && (letterDigitsVerify == true) && (digitsCountVerify == true)) { // check for the returned values
 System.out.println(\"It is a Valid Password.!\"); // message if is a valid password
 } else {
 System.out.println(\"Sorry. It is a Invalid Password.!\"); // message if it is not a valid password
 }
 }
 }
 OUTPUT :
Case 1 :
Please Enter the Password to Validate :
 johnpa
 Sorry. It is a Invalid Password.!
Case 2 :
Please Enter the Password to Validate :
 johnMis4
 Sorry. It is a Invalid Password.!
Case 3 :
Please Enter the Password to Validate :
 johnMart12
 It is a Valid Password.!
 Hope this is helpful.


