Click here to Skip to main content
15,662,823 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The question is that You have to generate a password
Hint to generate a security code is as follows :

Minimum of 8 Characters
Must contain atleast one uppercase, one lowercase and one special character.
Only the special characters @,*,# are allowed.

If the code fails to meet the criteria, it should response as
Invalid Security Code, Try Again!

What I have tried:

I wrote the following code:
import java.util.Scanner;
import java.util.regex.Pattern;

public class Main { 
    public static void main(String[] args) 
        Scanner scanner = new Scanner(;
        System.out.println("Generate your Security Code ");
        // reading security code from user
        String securityCode = scanner.nextLine();
        // regex pattern for given security code pattern
        // this pattern is matches if atleast 8 characters, one upper case,
        // one lower case and one special character from @,*,# 
        String pattern = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[#@*]).{8,}$";
        // checking whether security code is matching with pattern or not
        if (Pattern.matches(pattern, securityCode)) {
           // security code is matched with pattern
           System.out.println("Security Code Generated Successfully");
        } else {
           // security code not matched with pattern
           System.out.println("Invalid Security Code, Try Again!");
Updated 28-Sep-20 21:34pm

1 solution

Try it with this password:
It's 8 characters long, it contains a permitted special character, a lower case, and an upper case - but it's not valid.

Don't use a regex for this: Count upper, lower, permitted specials, and others separately, and then check the validity of the password from those counts: Regexes are great at pattern matching, but they don't count well, and you need to count!
The valid numbers are
upper                > 0
lower                > 0
permitted specials   > 0
others               = 0
upper + lower + permitted specials >= 8
You can't easily do that with a Regex, and if you do it becomes unmaintainable very, very quickly.
Share this answer
Ziad Ahmed Khan 29-Sep-20 6:30am    
Great answer I get your point but the allowed special characters are *,@,# the use of '&' will cause it to give ouput as Invalid
OriginalGriff 29-Sep-20 6:45am    
Yes, I know. That's what your Regex allows!
That's why you use counts instead: count the characters as groups, and then check teh group counts. A Regex won't let you do that.
Ziad Ahmed Khan 29-Sep-20 6:37am    
I used your logic and wrote the code as follows:

import java.util.Scanner;
public class SecurityCode {

public static void main(String[] args) {
Scanner reader = new Scanner(;
String code;
int upper = 0, lower = 0, special = 0;
int flag = 0;
//we take the security code input
System.out.print("Generate a security code: ");
code = reader.nextLine();
//we get the length of the string using the length() method
int len = code.length();
flag = 0;
//we check for the number of uppercase letters
//we check for the number of lowercase letter
//we check for a special character
//we do nothing if there is a digit
//we change flag to 1 character does not meet any of the criteria
//meaning we have a different kind of character
for(int i = 0; i<len; i++){
="" if(code.charat(i)="='@'" ||="" code.charat(i)="='#'" special++;
="" }else="" if(character.islowercase(code.charat(i))){
="" lower++;
="" if(character.isuppercase(code.charat(i))){
="" upper++;
="" if(character.isdigit(code.charat(i))){
="" }else{
="" flag="1;
" }
="" if="" our="" length="" is="" 8="" and="" higher="" upper,="" lower="" special="" characters
="" are="" there="" then="" the="" generation="" of="" security="" code="" successful
="" otherwise="" we="" take="" input="" again
="" if(len="">=8 && upper>0 && lower>0 && special>0 && flag == 0){
System.out.println("Security Code Generated Successfully.");
System.out.println("Invalid Security Code, Try Again!");

Now it fails for 2 test cases of Invalid password
OriginalGriff 29-Sep-20 6:50am    
Hint: Create four integer counts: Lower, Upper, Special, Other.
Create a loop to look at each character in the supplied password.
If the character is lowercase, increment Lower
If the character is uppercase, increment Upper
If the character is in the permitted specials, increment Special.
Otherwise, increment Other
After the loop, check the counts.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900