In: Computer Science
You are developing a software package for an online shopping
site 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 method that verifies that a password meets the stated
criteria. Use this method in a program that allows the user to
enter a password and then determines whether or not it is a valid
password. If it is valid, have the program print "Valid
Password". Otherwise, it should print "Invalid
Password".
Sample Run
java PasswordVerifier
Enter·password·to·be·verified:ComputerScience4Life↵
Valid·password
import java.util.Scanner;
public class PasswordVerifier {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter password to be verified: ");
String password = sc.nextLine();
if (validPassword(password))
System.out.println("Valid password");
else
System.out.println("Invalid password");
}
public static boolean validPassword(String pwd) {
return checkLength(pwd) && hasUppercase(pwd) && hasLowerCase(pwd) && hasDigits(pwd);
}
// checks if password has checkForUpperCasae
public static boolean hasUppercase(String pwd) {
for (int i = 0; i < pwd.length(); i++)
if (Character.isLetter(pwd.charAt(i)) && Character.isUpperCase(pwd.charAt(i)))
return true;
return false;
}
public static boolean hasLowerCase(String pwd) {
for (int i = 0; i < pwd.length(); i++)
if (Character.isLetter(pwd.charAt(i)) && Character.isLowerCase(pwd.charAt(i)))
return true;
return false;
}
// checks if password contains digit
public static boolean hasDigits(String pwd) {
for (int i = 0; i < pwd.length(); i++)
if (Character.isDigit(pwd.charAt(i)))
return true;
return false;
}
public static boolean checkLength(String s) {
return s.length() >= 6;
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot