5. Password Verifier Imagine you are developing a software package for Amazon.co
ID: 3934695 • Letter: 5
Question
5. Password Verifier
Imagine you are developing a software package for Amazon.com that requires users
to enter their own passwords. Your software requires that users’ passwords meet the
following criteria:
• The password should be at least six characters long.
• The password should contain at least one uppercase and at least one lowercase letter.
• The password should have at least one digit.
Write a class that verifies that a password meets the stated criteria. Demonstrate the class in
a program that allows the user to enter a password and then displays a message indicating
whether it is valid or not.
Explanation / Answer
PasswordCheck.java
import java.util.Scanner;
public class PasswordCheck {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(true){
System.out.print("Please enter the password: ");
String password = scan.next();
boolean status = validatePassword(password);
if(status == true){
System.out.println("Valid Password");
break;
}
else{
System.out.println("Invalid Password.. Try Again..");
}
}
}
public static boolean validatePassword(String pass){
int upperCount = 0;
int lowerCount = 0;
int digitCount = 0;
if(pass.length() >= 6){
for(int i=0; i<pass.length(); i++){
if(Character.isUpperCase(pass.charAt(i))){
upperCount++;
}
else if(Character.isLowerCase(pass.charAt(i))){
lowerCount++;
}
else if(Character.isDigit(pass.charAt(i))){
digitCount++;
}
}
if(upperCount > 0 && lowerCount >0 && digitCount >0){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
}
Output:
Please enter the password: password
Invalid Password.. Try Again..
Please enter the password: Pass1
Invalid Password.. Try Again..
Please enter the password: Pass
Invalid Password.. Try Again..
Please enter the password: Password
Invalid Password.. Try Again..
Please enter the password: Pass123
Valid Password
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.