In: Computer Science
[JAVA]
You will write a program to validate passwords for users, making
sure they meet the following criteria:
Rules:
Passwords must be at least 8 characters long
Passwords can only contain alpha numeric characters (no spaces or
special characters)
Passwords must contain at least 1 uppercase character
Passwords must contain at least 1 lowercase character
Passwords must contain at least 1 numeric character (0-9)
Passwords cannot contain the word “password”
A password that does not meet all of these rules must list each
rule that it breaks.
You must repeatedly ask the user for a password until the user
enters “endofinput”
This question was posted before however it used methods and I am not able to use methods for this assignment
Given below is the code for the question. Please do rate the answer if it helped. Thank you.
import java.util.Scanner;
public class PasswordValidator {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
String password;
System.out.print("Enter password:
");
password = input.nextLine();
boolean alphanumOk, ucaseOk,
lcaseOk, digitOk;
while(!password.equals("endofinput")) {
if(password.contains("password"))
System.out.println("Password should not contain
the word 'password'");
if(password.length() < 8)
System.out.println("Password should be atleast 8
characters long");
alphanumOk =
true;
ucaseOk =
false;
lcaseOk =
false;
digitOk =
false;
for(int i =
0; i < password.length(); i++) {
char c = password.charAt(i);
if(Character.isUpperCase(c))
ucaseOk = true;
else if(Character.isLowerCase(c))
lcaseOk = true;
else if(Character.isDigit(c))
digitOk = true;
else
alphanumOk = false;
}
if(!alphanumOk)
System.out.println("Passoword can not contain
special characters. Only alphanumeric characters allowed");
if(!lcaseOk)
System.out.println("Password should contain
atleast one lower case letter");
if(!ucaseOk)
System.out.println("Password should contain
atleast one upper case letter");
if(!digitOk)
System.out.println("Password should contain
atleast one digit");
if(alphanumOk
&& lcaseOk && ucaseOk && digitOk)
System.out.println("Password is good");
System.out.print("Enter password: ");
password =
input.nextLine();
}
}
}