Design and implement a Java program (name it PasswordTest) that accepts a string
ID: 3921184 • Letter: D
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 A good password will be at least 8 characters and includes at least one lower-case 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. For example, CS1301/99 is invalid password. The program should display the entered password and the program's Judgment on it. For example, Entered Password: CS1301/99 Verdict: Invalid Document your code, and organize and space out your outputs as shown.Explanation / Answer
PasswordTest.java
import java.util.Scanner;
public class PasswordTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter password: ");
String password = scan.next();
boolean status = false;
int lowerLetterCount = 0;
int upperLetterCount = 0;
int digitCount = 0;
int otherLetterCount = 0;
if(password.length() >= 8){
for(int i=0; i<password.length(); i++){
char ch = password.charAt(i);
if(ch >= 'a' && ch <= 'z'){
lowerLetterCount++;
}
else if(ch >= 'A' && ch <= 'Z'){
upperLetterCount++;
}
else if(ch >= '1' && ch <= '9'){
digitCount++;
}
else{
otherLetterCount++;
}
}
}
if(lowerLetterCount > 0 && upperLetterCount > 0 && digitCount > 0 && otherLetterCount > 0){
status = true;
}
String s = "";
if(status){
s = "Valid";
}else{
s = "Invalid";
}
System.out.println("Entered Password: "+password );
System.out.println("Verdict: "+s);
}
}
Output:
Enter password:
CS1301/99
Entered Password: CS1301/99
Verdict: Invalid
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.