Java Programming Implement a program that reads in a user pa

Java Programming

Implement a program that reads in a user password and verifies it meets the following criteria:

- At least 8 characters long

- Contains at lease 1 lower letter character

- Contains at least 1 upper letter character

- Contains at least 1 numeric digit

- Contains at least 1 special character from the set !@#$%^&*

- Does not contain the word \"and\" or the word \"the\"

- Promts the user for a password, including the requirements above in your output.

- Output a string that states valid or invalid. If invalid, state which rules from the list above have been met, and prompt the user to enter another password.

Utilize the following functionality:

- indexOf

- Looping structure

- charAT()

- isDigit()

- isUpperCase()

- isLowerCase()

Sample Output:

Password Verifier

Enter a password that meets the following rules:
<list rules from above>
Password1

Invalid
Missing a special character

-----------------------------------------------------------------

Password Verifier

Enter a password that meets the following rules:
<list rules from above>
Panda123$

Invalid
Contains the string “and”

-----------------------------------------------------------------

Password Verifier

Enter a password that meets the following rules:
<list rules from above>
Hi

Invalid
Must be at least 8 characters long
Must contain a numeric digit
Must contain a special character

-----------------------------------------------------------------

Password Verifier

Enter a password that meets the following rules:
<list rules from above>
Pa$$word1

Valid

Solution

PasswordCheckerUsingMethods.java

import java.util.Scanner;

public class PasswordCheckerUsingMethods {

   public static void main(String[] args) {
       //Declaring variable
       String str = null;
      
       // Scanner object is used to get the inputs entered by the user
       Scanner sc = new Scanner(System.in);
      
       //This loop continue to execute until the user enters a valid password
       while(true)
       {
          
           //Getting the input entered by the user
           System.out.print(\"Enter a new password :\");
           str = sc.nextLine();
           if(AtLeast8(str))
           {
               //Displaying the error message
               System.out.println(\"Invalid: Too Few Characters (8 at least)\");
               continue;
           }
           else if(!containsLowerCase(str))
           {
               System.out.println(\"Invalid.Contains at least 1 lower letter character\");
               continue;
           }
           else if(!containsUpperCase(str))
           {
               System.out.println(\"Invalid.Contains at least 1 upper letter character\");
               continue;
           }
          
           /* checking whether the password contains letter or digit by calling the method
           * if yes,then this block of code get executed and display
           * error message and prompt the user to enter again
           */
           else if (!containsDigit(str)) {
              
               //Displaying the error message
               System.out.println(\"Invalid: Contains at least 1 numeric digit\");
               continue;
              
           }
           else if(!containsSpecialChar(str))
           {
               System.out.println(\"Invalid:Missing a special character\");
               continue;
              
           }
           else if(containsInvalidWords(str))
           {
               System.out.println(\"Invalid.Does not contain the word \'and\'or the word \'the\'\");
               continue;
           }
           else
               System.out.println(\":: Valid Password ::\");  
           break;
       }
      

   }

   private static boolean containsInvalidWords(String str) {
       int flag=0;
   if(str.contains(\"and\") ||str.contains(\"the\") )
   {
       flag=1;
   }
   if(flag==1)
       return true;
   else
       return false;
   }

   private static boolean containsSpecialChar(String str) {
       int count=0;
       char arr[]={\'!\',\'@\',\'#\',\'$\',\'%\',\'^\',\'&\',\'*\'};
       for(int i=0;i<str.length();i++)
       {
           for(int j=0;j<arr.length;j++)
           {
               if(str.charAt(i)==arr[j])
               {
                   count++;
               }              
           }

       }
       if(count>=1)
       return true;
       else
       return false;
   }

   private static boolean containsDigit(String str) {
       int count=0;
       for(int i=0;i<str.length();i++)
       {
           if(Character.isDigit(str.charAt(i)))
           {
               count++;
           }
       }
       if(count>=1)
       return true;
       else
       return false;
   }

   private static boolean containsUpperCase(String str) {
       int count=0;
       for(int i=0;i<str.length();i++)
       {
           if(Character.isUpperCase(str.charAt(i)))
           {
               count++;
           }
       }
       if(count>=1)
       return true;
       else
       return false;

   }

   private static boolean containsLowerCase(String str) {
       int count=0;
       for(int i=0;i<str.length();i++)
       {
           if(Character.isLowerCase(str.charAt(i)))
           {
               count++;
           }
       }
       if(count>=1)
       return true;
       else
       return false;
   }

  

   /* This method will check whether the password length
   * is atleast 8 or not
   * Params : String
   * Return : boolean
   */
   private static boolean AtLeast8(String str) {
   if(str.length()<8)
       return true;
   else
       return false;
   }

  
}

___________________

Output:

Enter a new password :Password1
Invalid:Missing a special character
Enter a new password :Panda123$
Invalid.Does not contain the word \'and\'or the word \'the\'
Enter a new password :Hi
Invalid: Too Few Characters (8 at least)
Enter a new password :Pa$$word1
:: Valid Password ::

_____Thank You

Java Programming Implement a program that reads in a user password and verifies it meets the following criteria: - At least 8 characters long - Contains at leas
Java Programming Implement a program that reads in a user password and verifies it meets the following criteria: - At least 8 characters long - Contains at leas
Java Programming Implement a program that reads in a user password and verifies it meets the following criteria: - At least 8 characters long - Contains at leas
Java Programming Implement a program that reads in a user password and verifies it meets the following criteria: - At least 8 characters long - Contains at leas
Java Programming Implement a program that reads in a user password and verifies it meets the following criteria: - At least 8 characters long - Contains at leas

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site