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

Java Project You are developing a software package for an online shopping site t

ID: 664722 • Letter: J

Question

Java Project

You are developing a software package for an online shopping site that requiresusers to enter their own passwords. Your software requires that users' passwords meet the following criteria:

The password should be at least six characters long.
The password should contain at least one uppercase and at least one
lowercase letter.
The password should have at least one digit.

Write a method that verifies that a password meets the stated criteria. Use this method in a program that allows the user to enter a password and then determines whether or not it is a valid password. If it is valid, have the program print "Valid Password". Otherwise, it should print "Invalid Password".

Sample Output:

Enter password to be verified:ComputerScienceForLife
Invalid password

Explanation / Answer

import java.util.Scanner;

Please find the required solution:

public class ValidatePassword {
   /**
   * Method Validate input String based on following criteria
   * ------------------------------------------------------
   * The password should be at least six characters long.
   * The password should contain at least one uppercase
   * and at least one lowercase letter.
   * The password should have at least one digit.
   *
   * @param password
   * input string which needs to be validated
   * @return true if the input string meet the above criteria
   * otherwise return false
   */
   public static boolean validatePasssword ( String password )
   {
       // condition to check length of password
       if ( password.length() < 6 )
           return false;
       boolean containsUpper = false;
       boolean containsLower = false;
       boolean containsDigit = false;
       // iterate through each element in array(from string toCharArray())
       for ( char ch : password.toCharArray() )
       {
          
           // condition to check whether character in the current iteration is letter or digit
           if ( Character.isLetterOrDigit( ch ) )
           {
               // condition to check whether character in the current iteration is digit
               if ( Character.isDigit( ch ) )
                   containsDigit = true;
              
               //condition to check whether character in the current iteration is letter
               else if ( Character.isLetter( ch ) )
               {
                  
                   //condition to check whether character in the current iteration is in upper case
                   if ( Character.isUpperCase( ch ) )
                       containsUpper = true;
                  
                   //condition to check whether character in the current iteration is in upper case
                   else
                       containsLower = true;
               }
           }
       }
       if ( containsUpper && containsLower && containsDigit )
           return true;
       return false;
   }

   // Test Method to check above method
   public static void main ( String[] args )
   {
       Scanner scanner = new Scanner( System.in );
       System.out.println( "Enter password to be verified:" );
       String input = scanner.nextLine();
       if ( validatePasssword( input ) )
           System.out.println( "Valid Password" );
       else
           System.out.println( "InValid Password" );
       scanner.close();
   }
}

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