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 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 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...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Question 1: Write a program to receive a string from the user and compares the first...
Question 1: Write a program to receive a string from the user and compares the first and second half of the word and prints “First and Second Half Same” or “First and Second Half Different”. If the length of the word is odd, ignore the middle letter. For example: Run 1: Enter a word: abcdabc First and Second Half Same Run 2: Enter a word: abcde First and Second Half Different Hint: use the Math.floor() and length() to get the...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT