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...
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
•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...
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 Java program (name it PasswordTest) that reads from the user a string input (representing...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing a password) and determines whether the password is “Valid Password” or “Invalid Password”. A valid password has at least 7 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order to...
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...
Please write a JAVA program which reads a number n from the user and check whether...
Please write a JAVA program which reads a number n from the user and check whether n is a perfect number or not. For example, when n = 7, the print out should be 7 is not a perfect number. If the input n is 6, then the program prints out 6 = 1 * 2 * 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT