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

Java Programming Write a program that verifies that password rules are being fol

ID: 642073 • Letter: J

Question

Java Programming

Write a program that verifies that password rules are being followed properly.

Write a program that has method that checks whether a string is an acceptable password. Your main method will ask the user if he/she wants detailed information on the failure or not in the event a password is rejected; it must do this only once. Your main method will then request that the user enter a password to check and then it will invoke your method with a two inputs, the string the user entered, and a

Explanation / Answer

Program code:

//import Scanner class from util package

import java.util.*;

//VerifyPassword will verify the user input password and

//depending on the request the failure details are printed

public class VerifyPassword

{

     //main method

     public static void main(String args[])

     {

          //declare the required variables

          String userName;

          String passWord;

          boolean flag=false;

          boolean details;

          String detailedInfo;

          String choice;

          //loop till the user presses N or n

          do

          {

              //prompt the user to enter the user name and password and whether the user wants

              //details information

              Scanner input=new Scanner(System.in);     

              System.out.println("Enter the user name: ");

              userName=input.next();

              System.out.println("Enter the password: ");

              passWord=input.next();

              System.out.println("Wanted a detailed information on failure: Press(yes) ");

              detailedInfo=input.next();

              //if the user presses yes then details boolean variable is set to true

              //else set to false

              if(detailedInfo.equalsIgnoreCase("yes"))

              {

                   details=true;

              }

              else

              {

                   details=false;

              }

              //check whether the password meets the password verification criteria

              flag=checkPassword(passWord, details);

              //if the passWord does not meet the criteria then print the

              //details

              if(flag==false)

              {

                   System.out.println("The password is invalid");

              }

              //if the password meets the criteria then print user enter details

              else

              {

                   System.out.println("You have entered a valid password.");

                   System.out.println("User Name: "+userName);

                   System.out.println("Password: "+passWord);

              }

              //prompt the user whether the user wants to continue or not

              System.out.println("Would you like to verify other password: Press(Y/y)");

              choice=input.next();

          }while(!choice.equalsIgnoreCase("N"));

     }

     //checkPassword method takes password(String) and details(boolean) as input parameters

     public static boolean checkPassword(String pWord, boolean details)

     {

          //declare the required variables

          boolean flag1=true;

          boolean flag[]=new boolean[10];

          String strVerify="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()?.";

          int count=0;

          //check all the conditions and store the returned boolean value into respective flag array

          //store the boolean value whether the password is with the specified size

          flag[count++]=verifyLength(pWord);

          //store the boolean value whether the password contains the strVerify values

          flag[count++]=verifyAllCharacter(pWord.toLowerCase(),strVerify.toLowerCase());

          //store the boolean value whether the password contains the Two upper case letters or not

          flag[count++]=verifyAtleatTwoUpperCase(pWord);

          //store the boolean value whether the password contains the Two lower case letters or not'

          flag[count++]=verifyAtleatTwoLowerCase(pWord);

          //store the boolean value whether the password contains the Two numeric values

          flag[count++]=verifyAtleatTwoDigits(pWord);

          //store the boolean value whether the password contains one special character

          flag[count++]=verifyCharacters(pWord);

          //depending on the all the conditions

          //the boolean value is returned

          if(details)

          {

              printDetails(flag);

          }

          for(int i=0;i<count;i++)

              flag1=flag1 && flag[i];

          return flag1;

     }

     //verifyLength method verifies the length of the password is in between 8 and 24

     public static boolean verifyLength(String pword)

     {

          if(pword.length()>=8 && pword.length()<=24)

              return true;

          else

              return false;

     }

     //verifyAllCharacter method verifies all the required characters or not

     public static boolean verifyAllCharacter(String pword, String verify)

     {

          boolean flag[]=new boolean[25];

          boolean fg=true;

          int k=0;

          for(int i=0;i<pword.length();i++)

          {

              for(int j=0;j<verify.length();j++)

              {

                   if(pword.charAt(i)==verify.charAt(j))

                   {

                        flag[i]=true;

                        k++;

                   }                 

              }

              if(!flag[i])

                   flag[i]=false;

          }

          for(int i=0;i<pword.length();i++)

              fg=fg&&flag[i];

          return fg;

     }

     //verifyAtleatTwoUpperCase method verifies whether the password contains atleast

     //two upper case letters

     public static boolean verifyAtleatTwoUpperCase(String pword)

     {

          int count=0;

          String verify="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

          boolean fg=true;

          for(int i=0;i<pword.length();i++)

          {

              for(int j=0;j<verify.length();j++)

              {

                   if(pword.charAt(i)==verify.charAt(j))

                   {                      

                        count++;

                   }            

              }            

          }

          if(count>2)

              fg=true;

          else

              fg=false;

          return fg;        

     }

     //verifyAtleatTwoLowerCase method verifies whether the password contains atleast

     //two lower case letters

     public static boolean verifyAtleatTwoLowerCase(String pword)

     {

          int count=0;

          String verify="ABCDEFGHIJKLMNOPQRSTUVWXYZ";    

          verify=verify.toLowerCase();

          boolean fg=true;

          for(int i=0;i<pword.length();i++)

          {

              for(int j=0;j<verify.length();j++)

              {

                   if(pword.charAt(i)==verify.charAt(j))

                   {

                        count++;

                   }            

              }            

          }

          if(count>=2)

              fg=true;

          else

              fg=false;

          return fg;        

     }

     //verifyAtleatTwoLowerCase method verifies whether the password contains atleast

     //two numeric values

     public static boolean verifyAtleatTwoDigits(String pword)

     {

          int count=0;

          String verify="0123456789";          

          boolean fg=true;

          for(int i=0;i<pword.length();i++)

          {

              for(int j=0;j<verify.length();j++)

              {

                   if(pword.charAt(i)==verify.charAt(j))

                   {

                        count++;

                   }            

              }            

          }

          if(count>=2)

              fg=true;

          else

              fg=false;

          return fg;        

     }

     //verifyCharacters method verifies whether the password contains atleast

     //one special character or not

     public static boolean verifyCharacters(String pword)

     {

          int count=0;

          String verify="!@#$%^&*()?.";             

          boolean fg=true;

          for(int i=0;i<pword.length();i++)

          {

              for(int j=0;j<verify.length();j++)

              {

                   if(pword.charAt(i)==verify.charAt(j))

                   {

                        count++;

                   }            

              }            

          }

          if(count>0)

              fg=true;

          else

              fg=false;

          return fg;        

     }

     //printDetails method depending on the user request the reasons for the

     //invalid password are printed

     public static void printDetails(boolean flag[])

     {

          if(flag[0]==false)

              System.out.println("The length of the password should be inbetween 8 and 24.");

          if(flag[1]==false)

              System.out.println("The password contains characters other than A-Z, a-z, 0-9, !@#$%^&*()?.");

          if(flag[2]==false)

              System.out.println("The password should contain atleast two upper case letters.");

          if(flag[3]==false)

              System.out.println("The password should contain atleast two lower case letters.");

          if(flag[4]==false)

              System.out.println("The password should contain atleast two numeric values.");

          if(flag[5]==false)

              System.out.println("The password should contain atleast one special character(!@#$%^&*()?.).");

     }

}

Sample Output:

Enter the user name:

HenryPeter

Enter the password:

*ABCdef123*

Wanted a detailed information on failure: Press(yes)

yes

You have entered a valid password.

User Name: HenryPeter

Password: *ABCdef123*

Would you like to verify other password: Press(Y/y)

y

Enter the user name:

PeterParker@hotmail.com

Enter the password:

abc123**

Wanted a detailed information on failure: Press(yes)

yes

The password should contain atleast two upper case letters.

The password is invalid

Would you like to verify other password: Press(Y/y)

y

Enter the user name:

LeehomWong@gmail.com

Enter the password:

ABCD1234&&

Wanted a detailed information on failure: Press(yes)

yes

The password should contain atleast two lower case letters.

The password is invalid

Would you like to verify other password: Press(Y/y)

y

Enter the user name:

LeeWongHee@facebook.com

Enter the password:

ABCDefgh

Wanted a detailed information on failure: Press(yes)

yes

The password should contain atleast two numeric values.

The password should contain atleast one special character(!@#$%^&*()?.).

The password is invalid

Would you like to verify other password: Press(Y/y)

y

Enter the user name:

KeplerKingKong@twitter.com

Enter the password:

**12XYZabc0987%%

Wanted a detailed information on failure: Press(yes)

yes

You have entered a valid password.

User Name: KeplerKingKong@twitter.com

Password: **12XYZabc0987%%

Would you like to verify other password: Press(Y/y)

y

Enter the user name:

JKLOFP@google.com

Enter the password:

123ABC

Wanted a detailed information on failure: Press(yes)

yes

The length of the password should be inbetween 8 and 24.

The password should contain atleast two lower case letters.

The password should contain atleast one special character(!@#$%^&*()?.).

The password is invalid

Would you like to verify other password: Press(Y/y)

y

Enter the user name:

ABCD@xyz.com

Enter the password:

!#abcAGDE1234&&

Wanted a detailed information on failure: Press(yes)

no

You have entered a valid password.

User Name: ABCD@xyz.com

Password: !#abcAGDE1234&&

Would you like to verify other password: Press(Y/y)

N

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