In: Computer Science
Write a pro-active password checker that checks to make sure that a user entered password meets certain requirements. You must implement a simple program that prompts the user for two String values, a password and the same password again for confirmation. For the purposes of this lab, a legal password must have all of the following properties:
▪ Length of at least 8 characters
▪ Starts with a lower case letter
▪ Ends with a numerical digit
▪ Has one Uppercase
▪ Is exactly equal to the repetition of the password typed for confirmation
Complete your program to prompt the user to enter a password and a confirmation as explained above. If the password is shorter than 8 characters, print Password is too short! Otherwise, if the password does not start with a lower case letter, print Password must start with a lower case letter! Otherwise, if the password does not end with a digit, print Password must end with a digit! Otherwise, if the password does not match the confirmation, print Passwords do not match! If there is no Uppercase letter in the password print Password must contain uppercase letter! Finally, if all the conditions are satisfied, print Password is valid!
Must be in Java
The Java code to read a String from user and check if that string pass all the conditions set in the problem statement and then asks user to confirm password and checks if these two passwords are same is:
//Importing Scanner class to create a Scanner object to take user input
import java.util.Scanner;
public class PasswordCheck {
// spaces() function is used to format output
public static void spaces() {
System.out.printf(" ");
}
// input_password() function takes a String as input from user
public static String input_password() {
Scanner scnr = new Scanner(System.in);
String pass;
pass = scnr.nextLine();
return pass;
}
// convertstring() function converts string to char array
// String should be converted to char array because strings in java are immutable
// that means we can't access Strings by index
public static char[] convertstring(String convert) {
char[]con =new char[convert.length()];
for(int i=0; i< convert.length();i++)
con[i]=convert.charAt(i);
return con;
}
//Checks if fist character is lowercase letter
public static int check_lower(char c) {
if (Character.isLowerCase(c))
return 1;
else {
System.out.printf("Password must start with a lowercase letter!!!\n");
return -1;
}
}
//checks if last character is an integer
public static int check_integer(char c) {
int as = c;
if(as>=48 && as<=57)
return 1;
else {
System.out.printf("Password should end with a integer!!!\n");
return -1;
}
}
//Checks if password contain a Uppercase letter
public static int check_uppercase(char[] c) {
for(int i=0; i<c.length;i++) {
if(Character.isUpperCase(c[i]))
return 1;
}
System.out.printf("Password must contain atleast one uppercase letter!!!\n");
return -1;
}
//Checks if password length is greater than 7
public static int check_length(char[]c) {
if(c.length>=8)
return 1;
else {
System.out.printf("Password should contain atleast 8 character!!!\n");
return -1;
}
}
//Program execution starts here
public static void main(String[] args) {
//Printing password instructions
System.out.printf("Hi,\nYour password should contain: -->Length of at least 8 characters\n");
spaces();
System.out.printf("-->Starts with a lower case letter\n");
spaces();
System.out.printf("-->Ends with a numerical digit\n");
spaces();
System.out.printf("-->Has one Uppercase\n" );
//Creating loop variable flag, user_input
int flag=-1;
String user_input="";
//This while loop will read user entered password and checks all conditions
//By calling reqiured functions
//If all functions return 1, then password is valid
//Else the loop repeats till user enter valid password
while(flag==-1) {
System.out.printf("Set password: ");
user_input="";
user_input=user_input+input_password();
char []ch = new char[user_input.length()];
ch= convertstring(user_input);
int flag1= check_lower(ch[0]);
int flag2= check_integer(ch[ch.length-1]);
int flag3= check_uppercase(ch);
int flag4= check_length(ch);
if(flag1==1 && flag2==1 && flag3==1 && flag4==1) {
flag=0;
}
else {
System.out.printf("Try again!!!\n");
flag=-1;
}
}
//Once user enter valid password, we now asks user to confirm password
// and checks this password with previous one using .equals() function
// If both are equal then we display password is set
// Else this loop repeats till user enter correct password
String confirm_password;
int flag1=-1;
while(flag1==-1) {
System.out.printf("Confirm password: ");
confirm_password = input_password();
if(user_input.equals(confirm_password)) {
System.out.printf("Congratulations!! Your password is set");
flag1=0;
}
else {
System.out.printf("Passwords doesn't match!!! Try again\n");
}
}
}
}
I have used some special functions like charAt() , isLowerCase(), isUpperCase(), .length() to check the conditions given in problem statement. I have provided comments in the program to explain lines of code.
I have tested the code for all possible conditions and the code is doing good. I am sharing few output screenshots for your reference.
Hope the code helps you.
Thank you :)