Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that will Ask the user to enter a password Write a method that w

ID: 3723732 • Letter: W

Question

Write a program that will Ask the user to enter a password Write a method that will check whether the password follows a set of rules and will return true or false depending on the check. Here are the rules that ALL have to be true for the password to be correct: 1. Check whether the password has at least 8 characters. 2. Check whether the password has at least 2 numbers. 3. Check whether the password has at least 2 upper case letters 4 Check whether the password has at least 1 symbol (non-alphabets) 5. The password should not have any space. Now from the main method, send the user provided password to that method to check whether it follows the above set of rules and provide a corresponding prompt showing whether the password is in correct form or not

Explanation / Answer

PasswordValidate.java

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class PasswordValidate {
   public static void main(String[] args){
       String pwd;
       Scanner sc =new Scanner(System.in);
       //input password from user
       System.out.println("Enter the Password: ");
       pwd = sc.nextLine();
       //Call validate password from main function.Returns true if password is valid else return false.
       boolean chk = validatePassword(pwd);  
//Password is valid when true is returned
if(chk)
   System.out.println("Password is valid ");
   }

   private static boolean validatePassword(String pwd)
   {
       //check if password length is atleast 8
       if(pwd.length()<8)
       {
           System.out.println("Password length should be atleast 8");
           return false;
       }
       //Check if password has atleast one symbol
       // [a-zA-Z0-9] is the regular expression for password containing only numbers and alphabets
       Pattern p = Pattern.compile("[a-zA-Z0-9]*");
       Matcher m = p.matcher(pwd);
         
   if (m.matches()) {
           System.out.println("Invalid: Password should contain atleast one symbol");
           return false;
       }
      
       char ch;
       int num = 0;
       int a = 0;
       int space=0;
       for(int i=0;i<pwd.length();i++){
       ch=pwd.charAt(i);
       if(ch== ' ') space++; //counts whitespace
       if(Character.isDigit(ch)) num++; //counts digits
       if(Character.isUpperCase(ch)) a++; //counts number of uppercase letters
       }
       //check if password has atleast 2 numbers
       if(num<2)
       {
           System.out.println("Invalid: Password should contain atleast two numbers");
           return false;
       }
       //check if password has atleast 2 uppercase letters
       if(a<2)
       {
           System.out.println("Invalid: Password should contain atleast two uppercase letters");
           return false;
       }  
       //check if password has any whitespace
       if(space>0)
       {
           System.out.println("Invalid: Password should not contain any white spaces");
           return false;
       }
       return true;  
      
   }

}

Output

Enter the Password:
ASDer34#*
Password is valid

Enter the Password:
assd23@
Invalid: Password length should be atleast 8

Enter the Password:
asd34@#a
Invalid: Password should contain atleast two uppercase letters

Enter the Password:
ASdf123q
Password should contain atleast one symbol

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote