Search code examples
javapasswords

How can I get the system to exit if incorrect password is entered 3 consecutive times?


I am trying to insert a count method, where if the user input a password that doesn't match the required criteria in 3 attempts, the system will exit. I have tried to put a count function, where each time the password is incorrect 1 is added to the count, and when count == 3, system.exit. Can I get some advice why this is not working for me. The password conditions are, at least 10 characters, at least 1 upper case, either 2 or 3 numbers and only 1 special Character.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Test2 {
public static void main(String[] args) {
        int count = 0;
        
        System.out.println("Welcome to Humber College");
        System.out.println(" ");
        System.out.println(" ");
        
         Scanner in = new Scanner(System.in);
            System.out.print("Please enter a given  password : ");
            String password = in.nextLine();
            

            List<String> errorList = new ArrayList<String>();

            while (!isValid(password,  errorList)) {
                System.out.println("The password entered here  is invalid");
                count = count++;
                if (count == 3)
                {
                    System.exit(count);
                }
                
                for (String error : errorList) {
                    System.out.println(error);
                }

                System.out.print("Please enter a given  password : ");
                password = in.nextLine();
                
                
            }
            System.out.println("your password is: " + password);

        }

        public static boolean isValid(String password, List<String> errorList) {

            Pattern specailCharPatten = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
            Pattern UpperCasePatten = Pattern.compile("[A-Z ]");
            Pattern digitCasePatten = Pattern.compile("[0-9 ]");
            errorList.clear();
            
            
            boolean flag=true;

         
            if (password.length() < 11) {
                errorList.add("Password should not be less than 10 characters");
                System.out.println(" ");
                flag=false;
            }
            if (!UpperCasePatten.matcher(password).find()) {
                errorList.add("Password must have atleast one uppercase character");
                System.out.println(" ");
                flag=false;
            }
            if (!digitCasePatten.matcher(password).find()) {
                errorList.add("Password should contain 2 or 3 numbers");
                System.out.println(" ");
                flag=false;
            }
            if (!specailCharPatten.matcher(password).find()) {
                errorList.add("Password must have 1 speacial character");
                flag=false;
            }
            
           
            return flag;

    }

}

Solution

  • It's not a right way to increment variable.

    You can follow the following ways to increment or decrement variables: Let's say you have an integer count.

    First Way:

    • count++;

    Second Way

    • count = count + 1;

    Third Way

    • count += 1;