Kivan Ilangakoon
Kivan Ilangakoon

Reputation: 457

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;

    }

}

Upvotes: 0

Views: 426

Answers (3)

Rajan Gautam
Rajan Gautam

Reputation: 407

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;

Upvotes: 1

rzwitserloot
rzwitserloot

Reputation: 103244

count++ is java for 'increment count, and then this expression resolves to the value of count before it incremented'.

So, when you write:

count = count++;

and, let's say count is 5, then this code first increments count (it is now 6), then the expression resolves as '5', and then you assign that to be the new value of count. So, now, count is still 5.

The right way to increment is just count++. not count = count++. If you insist on using =, you'd write count = count + 1, or count += 1 - it's all identical.

Upvotes: 1

Mushroomator
Mushroomator

Reputation: 9218

The reason is that your counter is not updated and stays at 0 so that if never triggers. Just use count++; instead of count = count++; in your while loop and it will work like you expect it to.

Upvotes: 0

Related Questions