0

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");

    }
}
Manuel
  • 3,828
  • 6
  • 33
  • 48
  • 2
    You're doing way too much work in your loop. – tkausl May 08 '20 at 15:31
  • 2
    You can't tell that there aren't enough digits until *after* you have scanned the whole string. – Scott Hunter May 08 '20 at 15:31
  • See [here](https://stackoverflow.com/questions/26991679/the-best-way-to-match-at-least-three-out-of-four-regex-requirements) and [here](https://stackoverflow.com/questions/60443766/3-out-of-4-conditions-in-regex-java/60443927#60443927) for some related/alternative approaches. – andrewJames May 11 '20 at 13:22

3 Answers3

2

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.

Omaha
  • 2,262
  • 15
  • 18
1

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

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;
    }
VHS
  • 9,534
  • 3
  • 19
  • 43