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

Create a program Password that prompts for a password from the user and determin

ID: 659181 • Letter: C

Question

Create a program Password that prompts for a password from the user and determines if the password is valid or invalid. There should be a static method isValidPassword that validates the password (String) and returns either true or false.
A valid password follows these three rules:

- Must have at least eight characters.

- Must contain only letters and digits.

- Must contain at least two digits.

Use dialog boxes for the program. You must use the String and Character classes for this program. You may not use the regular expressions. The program only needs to run once. **If the password is invalid, allow the user three tries total to enter the valid password.

Explanation / Answer

Password.java

import javax.swing.JOptionPane;

public class Password {

   public static void main(String[] args) {

       int retryCount = 0;

       while (retryCount < 3) {

           retryCount++;

           String password = JOptionPane.showInputDialog("Please ENTER THE PASS WORD");

           if (isPasswordValid(password)) {

               JOptionPane.showMessageDialog(null, "Correct Password!!!");

               break;

           } else {

               JOptionPane.showMessageDialog(null, "Invalid Password");

               continue;

           }

       }

   }

   public static boolean isPasswordValid(String password) {

       if (password.length() >= 8 & isAlphanumeric(password) && isContains2digits(password))

           return true;

       else

           return false;

   }

   public static boolean isAlphanumeric(String string) {

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

           char ch = string.charAt(i);

           if (ch < 0x30 || (ch >= 0x3a && ch <= 0x40) || (ch > 0x5a && ch <= 0x60) || ch > 0x7a)

               return false;

       }

       return true;

   }

   public static boolean isContains2digits(String string) {

       int count = 0;

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

           char c = string.charAt(i);

           if (Character.isDigit(c)) {

               count++;

           }

           if (count > 2)

               return true;

       }

       return false;

   }

}

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