-1

IDFC, a leading Bank sector offers online shopping to their customers, To avail this offer, the customer will have to generate a security code, for the first time usage of his card to purchase online.

Hint to generate a security code is as follows :

1.Minimum of 8 Characters

2.Must contain atleast one uppercase, one lowercase and one special character.

  1. Only the special characters @,*,# are allowed.

The code fails to meet the criteria, will response with an error message as shown in the sample output.

Develop an application to implement this scenario.

Write a public class Main with the main method. Write the code to get the input, validate and print the output.

Sample Input 1:

Generate your Security Code

Ab12345@

Sample Output 1:

Security Code Generated Successfully

Sample Input 2:

Generate your Security Code

S1995p123

Sample Output 2:

Invalid Security Code, Try Again!

I was able to come up with a code which basically passes all the visible condition, but failing to test one. Not really sure where I am going wrong.

import java.util.*;
import java.util.regex.*;

class Main{
        public static void main(String a[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Generate your Security Code");
        String code = sc.next();
        String regex = "[A-Za-z0-9@*#]{8}";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(code);
        boolean matchFound = m.find();
        if(matchFound)
        System.out.println("Security Code Generated Successfully");
        else
        System.out.println("Invalid Security Code, Try Again!");
    }
}
Evaluation Result:
Proposed grade: 75 / 100
Result Description
Failed tests
Test 3: Invalid Password

Test 1: Valid Password

Summary of tests
 
*Note: All the test cases might not have same weightage
 
4 tests run/ 3 tests passed 
heisenberg
  • 11
  • 1
  • 3
  • You should probably say what you did to try to fix your answer. For example, did you try the sample inputs? In your case you have a regexp that accepts the right characters but doesn't test any of the other requirements (at least one cap, at least one lower case, at least one special character). – PeterK Nov 13 '20 at 16:27

1 Answers1

2

since it says that it has a minimum of 8 characters you must use {8,} this means 8 or more characters.

with your current regex, ab12345@ is accepted and as you can see there is no upper case character so a better regex would be:

^(?=.*[@*#])(?=.*[a-z])(?=.*[A-Z])[A-Za-z0-9@*#]{8,}$

Alireza
  • 2,103
  • 1
  • 6
  • 19