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 J

ID: 3859219 • Letter: J

Question

Java Program Needed

Please pay extra attention to bold items below:

Please use Java Language, WHILE LOOP and BOOLEAN to write the program. 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

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.july;

import java.util.Scanner;

/**
*
* @author Sam
*/
public class PasswordValidation {
    public static void main(String[] args) {
        String password;
        boolean err;
        Scanner in = new Scanner(System.in);
        do {
            System.out.print("Enter a new password: ");
            password = in.nextLine();
            int len = password.length();
            int digitCount = 0;
            int charCount = 0;
            err = false;
            while (charCount < len) {
                char ch = password.charAt(charCount);
                if (!Character.isLetterOrDigit(ch) && !err) {
                    System.out.println("Invalid: Only letters and digits");
                    err = true;
                }
                if (Character.isDigit(ch))
                    digitCount++;
                charCount ++;
            }
            if (charCount < 8 && !err) {
                System.out.println("Invalid: Too Few Characters (8 at least)");
                err = true;
            }
            if (digitCount < 2 && !err) {
                System.out.println("Invalid: At least two digits");
                err = true;
            }
        }while(err);
        System.out.println("Valid password!");
    }
}


Check this

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