66.8k views
5 votes
Write an application that prompts the user for a password that contains at least two uppercase letters, at least three lowercase letters, and at least one digit. Continuously reprompt the user until a valid password is entered. Display a message indicating whether the password is valid; if not, display the reason the password is not valid. Save the file as ValidatePassword.java.

User Rudder
by
9.1k points

1 Answer

2 votes

Answer:

Here's an example Java application that prompts the user for a password that contains at least two uppercase letters, at least three lowercase letters, and at least one digit. The program continuously reprompts the user until a valid password is entered, and displays a message indicating whether the password is valid or not, along with the reason why it's not valid:

import java.util.Scanner;

public class ValidatePassword {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String password;

boolean hasUppercase = false;

boolean hasLowercase = false;

boolean hasDigit = false;

do {

System.out.print("Enter a password that contains at least 2 uppercase letters, 3 lowercase letters, and 1 digit: ");

password = input.nextLine();

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

if (Character.isUpperCase(password.charAt(i))) {

hasUppercase = true;

} else if (Character.isLowerCase(password.charAt(i))) {

hasLowercase = true;

} else if (Character.isDigit(password.charAt(i))) {

hasDigit = true;

}

}

if (password.length() < 6) {

System.out.println("The password is too short.");

} else if (!hasUppercase) {

System.out.println("The password must contain at least 2 uppercase letters.");

} else if (!hasLowercase) {

System.out.println("The password must contain at least 3 lowercase letters.");

} else if (!hasDigit) {

System.out.println("The password must contain at least 1 digit.");

}

hasUppercase = false;

hasLowercase = false;

hasDigit = false;

} while (password.length() < 6 || !hasUppercase || !hasLowercase || !hasDigit);

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

}

}

When you run this program, it will prompt the user to enter a password that meets the specified criteria. If the password is not valid, the program will display a message indicating the reason why it's not valid and prompt the user to enter a new password. The program will continue to reprompt the user until a valid password is entered. Once a valid password is entered, the program will display a message indicating that the password is valid.

I hope this helps!

Step-by-step explanation:

User Aifuwa
by
8.1k points