Question

Please help me with this program in Java: Write a Java program that prompts the user...

Please help me with this program in Java: Write a Java program that prompts the user to enter a password that matches a specific pattern. Your program must approve the user's entry.. Here is the pattern, in this order: 1 or more upper case letters two lower case letters 1 or 2 digits zero or 1 upper case letters any two of this group @#$%^&

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for you:

import java.util.*;
class PasswordEvaluator
{
//Here is the pattern, in this order:
//1 or more upper case letters
//two lower case letters
//1 or 2 digits
//zero or 1 upper case letters
//any two of this group @#$%^&
public static boolean isValid(String password)
{
String symbol = "@#$%^&";
boolean accepted = false;
//boolean hasLength = false;
int upperCaseCount = 0;
int lowerCaseCount = 0;
int digitCount = 0;
int symbolCount = 0;
for(int i = 0; i < password.length(); i++) //For each character.
{
char c = password.charAt(i);
if(Character.isUpperCase(c))        //If uppercase, increment counter.
upperCaseCount++;
if(Character.isLowerCase(c)) //If lowercase, increment counter.
lowerCaseCount++;
if(Character.isDigit(c))            //If digit, increment counter.
digitCount++;
if(symbol.indexOf(c) != -1)       //If has symbol, mark flag.
symbolCount++;   
}
  
{
if(upperCaseCount < 1)
{
System.out.println("Password should contain 1 or more uppercase letter.");
return false;
}
if(symbolCount != 2)
{
System.out.println("Password should contain any two of special characters from @, #, $, %, ^, &");
return false;
}
if(lowerCaseCount != 2)
{
System.out.println("Password should contain 2 lowercase letter.");
return false;
}
if(digitCount != 2 && digitCount != 2)
{
System.out.println("Password should contain 1 or 2 digits.");
return false;
}
return true;
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while(true) {
//System.out.print("Do you want detailed information on failure or not...? ");
System.out.print("Choose (Y)es or (N)o...: ");
char c = (in.next()).charAt(0);
System.out.println("Enter your password.....");
System.out.println("Should contain one or more Uppercase letter, two lowercase letter");
System.out.print("1 or 2 digits, any two of the following symbol @, #, $, %, ^, & : ");
String password = in.next();
boolean flag = isValid(password);
if(flag)
System.out.println("The password is Acceptable.");
else
System.out.println("The password is not Acceptable. Its easy to guess.");
System.out.println("Do you want to continue checking for another password...? ");
System.out.print("Choose (Y)es or (N)o...: ");
c = (in.next()).charAt(0);
if(c != 'N' && c != 'n')
continue;
return;
}
}
}

And the output screenshot is:

Add a comment
Know the answer?
Add Answer to:
Please help me with this program in Java: Write a Java program that prompts the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of...

    NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of EXACTLY 4 hex digits, no more, no less, converts the hexadecimal value to its decimal value using string, character parsing, and Math methods learned in this chapter, and then prints the original string and its converted decimal value. NOTE: Hex digits may be in UPPER or lower case.

  • Write a program that separately prompts the user for a first name and last name and...

    Write a program that separately prompts the user for a first name and last name and outputs a string containing the following information, in order: a. First letter of the user's name. b. First five letters of the user's last name. c. A random two-digit integer You must construct the desired string ensuring all characters are lowercase; output the identification string accordingly. Assume the last name contains at least 5 characters. You must use the Random (java.util.Random) class to generate...

  • FOR JAVA: Summary: Write a program to assess password stringency. The solution should be named Password.java....

    FOR JAVA: Summary: Write a program to assess password stringency. The solution should be named Password.java. Include these steps: Write an application that provides the criteria for a strong password, and accepts a user's password from an input dialog box. If the entered password is less than six characters, more than 10 characters, or does not contain at least one uppercase letter and one digit, prompt the user to enter again. When the user's entry meets all the password requirements,...

  • please use java Write a program that prompts the user to enter line of text (including...

    please use java Write a program that prompts the user to enter line of text (including whitespace). The program then replaces the following vocals with numbers: a = 1, e = 2, i = 3, o = 4, u = 5 and outputs the result to the console. The program does not consider case, so a = 1, A = 1, e = 2, E = 2, etc. Example: Input: Hello, how are you? Output: H2ll4, h4w 1r2 y45? The...

  • i need know how Write a program that tests whether a string is a valid password....

    i need know how Write a program that tests whether a string is a valid password. The password rules are: It must have at least eight characters. It must contain letters AND numbers It must contain at least two digits It must contain an underscore (_) Write a program that prompts the user to enter a password and displays the password entered and whether it is valid or invalid.

  • Write a program that prompts a user for an input string of length at least two...

    Write a program that prompts a user for an input string of length at least two and prints the string with the first half in upper case and the second half in lower case. If the length of the string is odd, the "second half" should be one character longer than the "first half". Show Hint

  • CSC-295 Spring 2019 Assignment – String, StringBuilder and StringBuffer Write an application that prompts the user...

    CSC-295 Spring 2019 Assignment – String, StringBuilder and StringBuffer Write an application that prompts the user for a password that contains at least two uppercase letters, at least three lowercase letters, and at least one digit. Continuously reprompt the user until a valid password is entered. Display Valid password if the password is valid; if not, display the appropriate reason(s) the password is not valid as follows: The password did not have enough of the following: uppercase letters lowercase letters...

  • write a java program that displays the following: Write a Java program that prompts the user...

    write a java program that displays the following: Write a Java program that prompts the user to enter an integer and determines whether 1. it is divisible by 5 and 6, whether on 2. it is divisible by 5 or 6, 3. it is divisible by 5 or 6, but not both. Here is a sample run of this program: Sample run: Enter an integer: 10 Is 10 divisible by 5 and 6? false Is 10 divisible by 5 or...

  • In Java - Write a program that prompts the user to enter two integers and displays...

    In Java - Write a program that prompts the user to enter two integers and displays their sum. If the input is incorrect, prompt the user again. This means that you will have a try-catch inside a loop.

  • in java 3) Sum. Write a program that prompts the user to read two integers and...

    in java 3) Sum. Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT