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

Java Program Needed Please pay extra attention to bold items below: . Please use

ID: 3859253 • Letter: J

Question

Java Program Needed

Please pay extra attention to bold items below:

.

Please use Java Language, Everthing must be done under main methods meaning do not create extra methods in the code. use WHILE LOOP and BOOLEAN(True/False) in the program so that way the program should stop only when the user enters a valid password.. Also please use Character.isLetterOrDigit(ch) to check if ch is letter or digit. You can use Character.isDigit(ch)

.

Suppose a website impose the following rules for passwords.

A password must have at least eight characters.

A password consists of only letters and digits.

A password must contain at least two digits.

.

Write a program that prompts the user to enter a password. If the user enters an invalid password, the program needs to tell the user why it is invalid and ask the user to enter another password. The program should stop only when the user enters a valid password.

Here is one sample run to check these 4 passwords in order:

Enter a new password: My password18

Invalid: Only letters and digits

Enter a new password: pass18

Invalid: Too Few Characters (8 at least)

Enter a new password: password

Invalid: At least two digits

Enter a new password: Mypassword18

Valid password!

.

NOTE:

Start small.

Try to add one rule at a time. Make sure this rule is implemented correctly before adding the next one.

Make your program work for handling one password first. Then, add the while loop to handle potentially multiple passwords so that the program will stop only when a valid password is typed.

For a password that violates multiple rules, you only need to let the user know the first rule that is violated.

Suppose ch takes one character, you can use Character.isLetterOrDigit(ch) to check if ch is letter or digit. Moreover, you can use Character.isDigit(ch) to check if ch is a digit; increase the count of digits by one only when one digit is found.

Explanation / Answer

PasswordDemoTest.java

import java.util.Scanner;

public class PasswordDemoTest {

public static void main(String[] args) {

Scanner scan= new Scanner(System.in);

System.out.print("Enter a new password: ");

String password = scan.nextLine();

boolean isValid = false;

boolean isErrorMsgDisplayed = false;

int digitCount = 0;

while(!isValid) {

isErrorMsgDisplayed = false;

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

char ch = password.charAt(i);

if(!Character.isLetterOrDigit(ch)) {

System.out.println("Invalid: Only letters and digits");

isErrorMsgDisplayed = true;

break;

} else if(password.length() < 8) {

System.out.println("Invalid: Too Few Characters (8 at least)");

isErrorMsgDisplayed = true;

break;

} else if(Character.isDigit(ch)) {

digitCount++;

}

}

if(digitCount < 2 ) {

if(!isErrorMsgDisplayed) {

System.out.println("Invalid: At least two digits");

}

} else {

System.out.println("Valid password!");

isValid = true;

}

if(!isValid) {

System.out.print("Enter a new password: ");

password = scan.nextLine();

}

}

}

}

Output:

Enter a new password: My password18
Invalid: Only letters and digits
Enter a new password: pass18
Invalid: Too Few Characters (8 at least)
Enter a new password: password
Invalid: At least two digits
Enter a new password: Mypassword18
Valid password!

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