Edoardo
Edoardo

Reputation: 1

Checking Password in java. It must contains at least 10 letters (numbers or digits) and at least 2 digits

I have this problem with my code. It reads if the password has at least 10 characters but about the digits, it doesn't work. It always tells me that the password must contain at lease 2 digits.

public class EsercizioPassword {

    static int passLength = 10;
    static String pass = "hello1221";

    public static void main(String[] args) {


        checkPass();
    }

    public static void checkPass() {
        int digit=0;
        for (int i = 0; i < pass.length(); i++) {
            if(Character.isDigit(pass.charAt(i))) {
                digit++;
                if(digit<2) {
                    System.out.println("At least 2 digits");
                    return;
                }
            }
            if (!Character.isLetterOrDigit(pass.charAt(i))) {
                System.out.println("Password must contains only Lettere or Digit ");
                return;
            }else if (pass.length() < 10) {
                System.out.println("Password must contains at least 10 characters");
                return;
            }

        }
        System.out.println("Correct password");

    }
}

Upvotes: 0

Views: 1122

Answers (3)

VHS
VHS

Reputation: 10184

You should use a character array instead of a String to store password as a good programming practice. Also, avoid multiple return statements. It makes the program hard to read. See the following snippet. I haven't compiled or run it. But it should work mostly.

 public static boolean checkPass(char[] pass) {
        boolean isValid = true;
        int digit=0;

        if(pass.length <10) {
            isValid = false;
            System.out.println("Password must contains at least 10 characters");
        }
        else {
            for (int i = 0; i < pass.length; i++) {
                if (!Character.isLetterOrDigit(pass[i]))) {
                    System.out.println("Password must contains only Lettere or Digit ");
                    isValid = false;
                    break;
                }
                else if(Character.isDigit(pass[i])) {
                    digit++;
                    if(digit==2) {
                       break;
                    }
                }
            }
            if (digit != 2) {
                  System.out.println("At least 2 digits");
                  isValid =false;
            }
        }
        if(isValid)
            System.out.println("Correct password");    
        return isValid;
    }

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521389

Just to round out all the answers, I thought I would post a regex based solution, which really just needs a one liner:

public static boolean checkPass() {
    return pass.matches("(?=.{10,}).*\\d.*\\d.*");
}

The regex pattern used here is (including the ^ and $ anchors which are added by String#matches()):

^(?=.{10,}).*\d.*\d.*$

This pattern asserts that the length is 10 or more, and that at least two of the password characters are digits.

Upvotes: 1

Omaha
Omaha

Reputation: 2292

You're checking the digit count as soon as you increment it. You should verify that digit>=2 after the for loop exits.

You do the same thing for the length verification. Pull that out of the loop too.

Upvotes: 2

Related Questions