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

Write a Java program which does: Prompt user to input a password that follows th

ID: 3937437 • Letter: W

Question

Write a Java program which does:

Prompt user to input a password that follows the password requirements below;

Verify the password to see if it satisfy the requirements. If not, display it with error message and prompt the user again until it fully complies the requirements;

Print out the password.


* Note: Algorithm of the program is required (using comments at the beginning of the program)  

Password Requirements:

1. At least 8 characters long

2. Contains at least one upper-case letter (from 'A' to 'Z')

3. Contains at least one lower-case letter (from 'a' to 'z')

4. Contains at least one of these special characters (!, ", #, $, %, and &)

5. Contains at least one numerical digit (from '0' to '9')

Explanation / Answer

PasswordCheck.java


import java.util.Scanner;


public class PasswordCheck {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       while(true){
       System.out.print("Please enter the password: ");
       String password = scan.next();
       boolean status = validatePassword(password);
       if(status == true){
           System.out.println("Valid Password");
           break;
       }
       else{
       System.out.println("Invalid Password.. Try Again..");  
       }
       }
   }
   public static boolean validatePassword(String pass){
       int upperCount = 0;
       int lowerCount = 0;
       int digitCount = 0;
       int specialCharCount = 0;
       String specialChar = "!"#$%&";
       if(pass.length() >= 8){
           for(int i=0; i<pass.length(); i++){
               if(Character.isUpperCase(pass.charAt(i))){
                   upperCount++;
               }
               else if(Character.isLowerCase(pass.charAt(i))){
                   lowerCount++;
               }
               else if(Character.isDigit(pass.charAt(i))){
                   digitCount++;
               }
               else if(specialChar.contains(""+pass.charAt(i))){
                   specialCharCount++;
               }
           }
           if(upperCount > 0 && lowerCount >0 && digitCount >0 && specialCharCount>0){
               return true;
           }
           else{
               return false;
           }
              
       }
       else{
           return false;
       }
      
   }

}

Output:

Please enter the password: suresh
Invalid Password.. Try Again..
Please enter the password: password
Invalid Password.. Try Again..
Please enter the password: Password
Invalid Password.. Try Again..
Please enter the password: Password1
Invalid Password.. Try Again..
Please enter the password: Password1!
Valid Password

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