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

Write a java program called CheckPassword that asks a user to enter a password s

ID: 3759003 • Letter: W

Question

Write a java program called CheckPassword that asks a user to enter a password string and tells the user whether or not that string would make a valid password based on the following set of rules:

A password must have at least eight characters.

A password consists of only letters and digits.

A password must contain at least two digits.

Your program will consist of a main() method and three other methods, each of which return a boolean, and each of which will determine if one of the conditions is met. The three boolean methods will be called has8Characters(), isAlphaNumeric() andhas2Digits(), and they will have the following method headers:

Design the main() method of your program such that it asks the user to enter a password string, and then it passes the string to the three methods, one at a time, and based on the return value from those methods, your program prints either "Valid Password" or "Invalid Password".

Also, design the main() method of your program such that it allows the user to re-run the program with different inputs (i.e., use a loop structure).

Here is a sample output:

Explanation / Answer

Solution:

package com.chegg.nancy.solutions;

import java.util.Scanner;

public class CheckPassword {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       String input;
       String isCont = null;
       try {
           do {
               System.out.println(" Enter a password string: = ");
               input = scan.next();

               boolean res1 = has8Characters(input);
               boolean res2 = isAlphaNumeric(input);
               boolean res3 = has2Digits(input);

               if (res1 && res2 && res3) {
                   System.out.println("Valid Password.");
               } else {
                   System.out.println("Invalid Password.");
               }
               System.out.println("Would you like to go again? Press N or n for 'no'.Press Y or y for 'yes': ");
               isCont = scan.next();
           } while (isCont.equalsIgnoreCase("Y"));
       } catch (Exception e) {
           System.out.println("Error!");
       } finally {
           scan.close();
           System.out.println(" Program Exited.");
       }
   }

   /** Determines if the string has at least 8 characters */
   public static boolean has8Characters(String str) {
       if (str.length() >= 8)
           return true;
       else
           return false;
   }

   /** Determines if the string has only letters and digits */
   public static boolean isAlphaNumeric(String str) {
       if (str.matches("[A-Za-z0-9]+"))
           return true;
       else
           return false;
   }

   /** Determines if the string has at least 2 digits */
   public static boolean has2Digits(String str) {
       int count = 0;
       for (int i = 0, len = str.length(); i < len; i++) {
           if (Character.isDigit(str.charAt(i))) {
               count++;
           }
       }

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

output:

Enter a password string: =
CS2301/03
Invalid Password.
Would you like to go again?
Press N or n for 'no'.Press Y or y for 'yes':
y


Enter a password string: =
21jumpstreet
Valid Password.
Would you like to go again?
Press N or n for 'no'.Press Y or y for 'yes':
n

Program Exited.

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