Question

In: Computer Science

Write a pro-active password checker that checks to make sure that a user entered password meets...

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

Solutions

Expert Solution

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 :)


Related Solutions

In PYTHON: Write a program that asks for a password from a user and checks if...
In PYTHON: Write a program that asks for a password from a user and checks if it satisfies the conditions: Contains at least one lowercase letter (a-z). Contains at least one uppercase letter (A-Z). Contains at least one digit (0-9). Minimum length 6 characters. Contains no spaces. If the password is valid, then the program prints “password accepted”. Otherwise, it keeps asking for more passwords from the user until a valid password is obtained. Hint: You might like to check...
Write a program that checks whether or not a date entered in by the user is...
Write a program that checks whether or not a date entered in by the user is valid. Display the date and say if it is valid. If it is not valid explain why. The input may be in the format mm/dd/yyyy, m/d/yyyy, mm/d/yyyy, or m/dd/yyyy. A valid month (mm) must be from 1 to 12 A valid day (dd) must be from 1 to the appropriate number of days in the month April, June, September, and November have 30 days...
[10, 3, 15, 18] 4. Password Checker Write a fuction called passwordChecker() that asks the user...
[10, 3, 15, 18] 4. Password Checker Write a fuction called passwordChecker() that asks the user to input a string and checks if the string can be used as a password. Loop until the user inputs an appropriate password. A password must satisfy the following conditions: (a) Must contain at least 12 characters (b) Must contain one special symbol from string.punctuation [HINTS: use the string module; use the keyword in ] (c) Must contain at least one lower-case letter (d)
Task Intro: Password JAVA. Write a method that checks a password. The rules for the password...
Task Intro: Password JAVA. Write a method that checks a password. The rules for the password are: - The password must be at least 10 characters. - The password can only be numbers and letters. - Password must have at least 3 numbers. Write a test class that tests the checkPassword method. Hint: You can (really should) use method from built-in String class: public boolean matches(String regex) to check that the current string matches a regular expression. For example, if...
c++ Develop a program that validates a password entered by a user. The password must meet...
c++ Develop a program that validates a password entered by a user. The password must meet the following criteria: Be at least six characters long. Contain at least one uppercase and at least one lowercase letter. Have at least one digit. Write a program that asks for a password and then verifies that it meets the stated criteria. If it does not, the program should display a message telling the user why and ask for another password. The program should...
Write a C++ program that checks if the password is correct. The password is a 4-digit...
Write a C++ program that checks if the password is correct. The password is a 4-digit number combination. The program repeats to ask the password until the password is correct or you enter -1 to exit. Input: The password is set to ‘1123’. A user input the password to proceed, or -1 to exit. Sample Output: The program should display the following output. (The red text is a user input.) Test case 1: when you enter the correct password. Enter...
Task Intro: Password JAVA and JUnit5(UNIT TESTING) Write a method that checks a password. The rules...
Task Intro: Password JAVA and JUnit5(UNIT TESTING) Write a method that checks a password. The rules for the password are: - The password must be at least 10 characters. - The password can only be numbers and letters. - Password must have at least 3 numbers. Write a test class(Junit5/Unit testing) that tests the checkPassword method. Hint: You can (really should) use method from built-in String class: public boolean matches(String regex) to check that the current string matches a regular...
Task Intro: Password JAVA and JUnit5(UNIT TESTING) Write a method that checks a password. The rules...
Task Intro: Password JAVA and JUnit5(UNIT TESTING) Write a method that checks a password. The rules for the password are: - The password must be at least 10 characters. - The password can only be numbers and letters. - Password must have at least 3 numbers. Write a test class(Junit5/Unit testing) that tests the checkPassword method. Hint: You can (really should) use method from built-in String class: public boolean matches(String regex) to check that the current string matches a regular...
Using C++ Write a program to ask user to enter a password and validity the password....
Using C++ Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space. Functions: You will need at least the...
In Python write a program that prompts the user for a file name, make sure the...
In Python write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT