Your assignment is to write a program that verifies that password rules are bein
ID: 3759693 • Letter: Y
Question
Your assignment is to write a program that verifies that password rules are being followed properly. Write a program that has method that checks whether a string is an acceptable password. Your main method will ask the user if he/she wants detailed information on the failure or not in the event a password is rejected; it must do this only once. Your main method will then request that the user enter a password to check and then it will invoke your method with a two inputs, the string the user entered, and a “detailed information wanted flag”. Your method will return a true if the password is acceptable and a false if the password is not acceptable. The main program must then display a message stating that the password is either acceptable or unacceptable (too easy to guess). In either case, the program will ask the user if he wants to check another password and will continue to do so until the user says no or (‘N’ or ‘n’, that is up to you). To be an acceptable password, the following rules must be obeyed: • The password must have at least eight characters. • The password must not have more than twenty-four characters. • The password must consist of only letters, digits and any of the special characters !@#$%^&*()?. • The password must have at least two upper case letters. • The password must have at least two lower case letters. • The password must have at least two digits. • The password must have at least one of the special characters. If the “detailed information wanted flag” is true, your method will display information stating which of the above criterial failed. If the flag is false, it will not display any information.
Explanation / Answer
import java.util.*;
class PasswordConformity
{
public static boolean validPassword(String password, char verbose)
{
String symbol = "!@#$%^&*()?";
boolean accepted = false;
boolean hasLength = false;
int upperCaseCount = 0;
int lowerCaseCount = 0;
int digitCount = 0;
boolean hasSymbol = false;
if(password.length() >= 8 && password.length() <= 24) //Checks for length.
hasLength = true;
for(int i = 0; i < password.length(); i++) //For each character.
{
char c = password.charAt(i);
if(Character.isUpperCase(c)) //If uppercase, increment counter.
upperCaseCount++;
if(Character.isLowerCase(c)) //If lowercase, increment counter.
lowerCaseCount++;
if(Character.isDigit(c)) //If digit, increment counter.
digitCount++;
if(symbol.indexOf(c) != -1) //If has symbol, mark flag.
hasSymbol = true;
}
if(verbose == 'N' || verbose == 'n') //If no verbose is required.
if(hasLength && upperCaseCount > 1 && lowerCaseCount > 1 && digitCount > 1 && hasSymbol && hasLength)//If conforms.
return true; //Returns true.
else
return false;
else //If verbose is required. Print the relative message.
{
if(!hasLength)
System.out.println("Password length should be between 8 and 24.");
if(upperCaseCount <= 1)
System.out.println("Password should contain minimum 2 uppercase letters.");
if(lowerCaseCount <= 1)
System.out.println("Password should contain minimum 2 lowercase letters.");
if(digitCount <= 1)
System.out.println("Password should contain minimum 2 digits.");
if(!hasSymbol)
System.out.println("Password should contain atleast one special characters from !, @, #, $, %, ^, &, *, (, ), ?");
if(hasLength && upperCaseCount > 1 && lowerCaseCount > 1 && digitCount > 1 && hasSymbol && hasLength)
return true;
else
return false;
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while(true) {
System.out.print("Do you want detailed information on failure or not...? ");
System.out.print("Choose (Y)es or (N)o...: ");
char c = (in.next()).charAt(0);
System.out.println("Enter your password.....");
System.out.println("Should contain 8 to 24 characters, two Uppercase letter, two lowercase letter");
System.out.print("two digit, at least one of the following symbol !, @, #, $, %, ^, &, *, (, ), ? : ");
String password = in.next();
boolean flag = validPassword(password, c);
if(flag)
System.out.println("The password is Acceptable.");
else
System.out.println("The password is not Acceptable. Its easy to guess.");
System.out.println("Do you want to continue checking for another password...? ");
System.out.print("Choose (Y)es or (N)o...: ");
c = (in.next()).charAt(0);
if(c != 'N' && c != 'n')
continue;
return;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.