: Design and implement a Java program (name it PasswordTest) that accepts a stri
ID: 3669213 • Letter: #
Question
: Design and implement a Java program (name it PasswordTest) that accepts a string from the user and evaluates it as a password, giving it a valid or invalid verdict. The program must use a loop to parse the password string. A good password will be at least 8 characters and includes at least one lowercase letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order to render a verdict (in other words, do not use the String class instance method matches(), the Pattern class static method matches(), or any other method which parses the entire password string). For example, CS1301/07 is an invalid password. The program should display the entered password and the program’s judgment as to its validity. For example: Entered Password: PS9501/10 Verdict: Invalid. Please no arrays thanks.
Explanation / Answer
-----------------------------------------------------
---------------------------------------------------------
public class PasswordStats {
public void printPasswordStats(String password) {
int numOfSpecial = 0;
int numOfLetters = 0;
int numOfUpperLetters = 0;
int numOfLowerLetters = 0;
int numOfDigits = 0;
byte[] bytes = password.getBytes();
for (byte tempByte : bytes) {
if (tempByte >= 33 && tempByte <= 47) {
numOfSpecial++;
}
char tempChar = (char) tempByte;
if (Character.isDigit(tempChar)) {
numOfDigits++;
}
if (Character.isLetter(tempChar)) {
numOfLetters++;
}
if (Character.isUpperCase(tempChar)) {
numOfUpperLetters++;
}
if (Character.isLowerCase(tempChar)) {
numOfLowerLetters++;
}
}
System.out.println("numOfSpecial = " + numOfSpecial);
System.out.println("numOfLetters = " + numOfLetters);
System.out.println("numOfUpperLetters = " + numOfUpperLetters);
System.out.println("numOfLowerLetters = " + numOfLowerLetters);
System.out.println("numOfDigits = " + numOfDigits);
}
public static void main(String[] args) {
new PasswordStats().printPasswordStats("pas$.WOor!/d1%");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.