Question

In: Computer Science

2. Write a Java program to read a string (a password)from the user and then   check...

2. Write a Java program to read a string (a password)from the user and then

  check that the password conforms to the corporate password policy.

  The policy is:

  1) the password must be at least 8 characters

  2) the password must contain at least two upper case letters

  3) the password must contain at least one digit

  4) the password cannot begin with a digit

  Use a for loop to step through the string.

  Output “Password OK” if the password conforms, otherwise

  output “Password does not conform to the policy.”

*****IN JAVA**********

Solutions

Expert Solution

Hi, Please find java code for given problem statement below:

import java.util.Scanner;
public class Main{
        public static void main(String[] args) {
            
            int passwordLength = 0, upperCaseCharacterCount = 0;
            boolean isDigitPresent = false,isBeginWithDigit = false;
            
            //Accepting password from user
            Scanner sc = new Scanner(System.in);
                System.out.print("Enter password : ");
                String password = sc.nextLine();  
                
                //Checking password given conditions.
                for (int i =0 ; i<password.length(); i++){
                    ++passwordLength;
                    char ch = password.charAt(i);
                    if (i == 0 && Character.isDigit(ch)){
                        isBeginWithDigit = true;
                        break;
                    }
                    if (Character.isUpperCase(ch))
                        ++upperCaseCharacterCount;
                    if (Character.isDigit(ch))
                        isDigitPresent = true;
                }
                //If all password conditions are true then print password OK
                if (passwordLength >= 8 && isDigitPresent && upperCaseCharacterCount >=2 && !isBeginWithDigit)
                        System.out.print("Password OK");
                else
                    System.out.println("Password does not conform to the policy");
        }
}

Also find screenshot of above code and its output for better understanding and indentation. Also have a look on comments as well

Output

Explanation:

  • First, program declare some variables which is used to track characters in entered password.
  • Then user is asked to enter the password using scanner class object.
  • After that program uses for loop to iterate over the characters of the entered password.
  • In for loop, program calculates the number of characters, checking if password contains digit, counting number of uppercase letters and checking if first character is digit.
  • After coming out of for loop, values of various computed varables is compared and if all conditions get satisfied then print Password OK else print Password does not conform to the policy.

Related Solutions

Write a Java method to check whether a string is a valid password. Password rules: A...
Write a Java method to check whether a string is a valid password. Password rules: A password must have at least ten characters. A password consists of only letters and digits. A password must contain at least two digits. There are at least SIX functions/methods as the following: 1. menu() – to print the password’s rules. 2. getString() – to get the password from the user. 3. isPassword() – to check either the password is valid based on the given...
•Write a JAVA program to check a given password strength from a user's input. •Create a...
•Write a JAVA program to check a given password strength from a user's input. •Create a method to check the number of characters. It must be more than 8. •Create a method to check the password to have at least one uppercase letter. •Create a method to check the password to have at least one lowercase letter. •Create a method to check the password to have at least one digit. •Create a method to check the password to have at...
•Write a JAVA program to check a given password strength from a user's input. •Create a...
•Write a JAVA program to check a given password strength from a user's input. •Create a method to check the number of characters. It must be more than 8. •Create a method to check the password to have at least one uppercase letter. •Create a method to check the password to have at least one lowercase letter. •Create a method to check the password to have at least one digit. •Create a method to check the password to have at...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
(JAVA) We will write a program to check the spelling of the word entered by user,...
(JAVA) We will write a program to check the spelling of the word entered by user, using data from a dictionary, which is text file. The program will ask user to enter a word for spell check, and then check in the text file (dictionary.txt) if the word exists. (The dictionary.txt file is provided) If the word exists in text file, the output will be “Word is correctly spelled”. If the word doesn’t exist in the text file, program will...
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
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...
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string...
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string that deletes each non-alphanumeric character in the original string. You should solve this problem in 2 ways, first use a for loop that iterates through each character in the string, the second should use a while loop that iterates through a range. Keep spaces in the new string also. Hint: You can invoke the isalnum() function on a character, and it will return True...
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT