-1

I'm a beginner in java and I have a problem. Why is " Password does not meet requirement: must contain only letters and digits" printing out two times? If the user puts Test## for example, the output above prints twice. When I put a password such as "asd" the error message "Password does not meet requirement: must be between 6 and 15 characters", only prints once. Which is what I want the second case to do. This is my code below, please feel free to tell me the issue. Thank you!

public static void main(String[] args) {
    // User input

    Scanner input = new Scanner(System.in);

    // Variables

    final String previousPassword1 = "secret007";
    final String previousPassword2 = "your2eyes";
    int amountofAttempts = 0;
    boolean validPassword = false;
    final int minLength = 6;
    final int maxLength = 15;

    while (!validPassword && amountofAttempts < 4) {
        amountofAttempts++;
        String yolo = getNewPassword(input);
        if (!lengthTest(yolo)) {
            printErrorMessage(1);
        } else if (onlyLettersAndDigitsTest(yolo, yolo));
        printErrorMessage(2);
    }
}

public static String getNewPassword(Scanner input) {
    System.out.println();
    System.out.println();
    System.out.print("Enter a password: ");
    String passWord = input.nextLine();
    return passWord;

}

public static boolean lengthTest(String length) {
    boolean validLength = false;
    if (length.length() >= 6 && length.length() < 15) {
        validLength = true;
    }
    return validLength;
}

public static boolean onlyLettersAndDigitsTest(String digandlet, String length) {
    for (int i = 0; i < length.length(); i++) {
        if (Character.isLetterOrDigit(digandlet.charAt(i))) {
            return false;
        }
    }
    return true;
}


public static boolean containsOneToThreeDigitsTest(String password) {

    int amountOfDigits = 0;
    for (int i = 0; i < password.length(); i++) {
        if (Character.isDigit(password.charAt(i))) {
            amountOfDigits++;
        }
    }
    if (amountOfDigits > 3 || amountOfDigits < 1) {
        return false;
    }
    return true;
}



public static boolean differentThanLastTwoPasswordsTest(String previousPassword1, String previousPassword2, String password) {
    if (password.equals(previousPassword1) || password.equals(previousPassword2)) {
        return false;
    } else {
        return true;
    }
}

public static void printErrorMessage(int errorCode) {
    switch (errorCode) {
        case 1:
            System.out.print("Password does not meet requirement: must be between 6 and 15 characters");
            break;

        case 2:
            System.out.print("Password does not meet requirement: must contain only letters and digits");

        case 3:
            System.out.print("Password does not meet requirement: must contain at least 1 digit and not more than 3");
            break;

        case 4:
            System.out.print("Password does not meet requirement: must be different than previous 2 passwords");
            break;
    }
}
}
selbie
  • 100,020
  • 15
  • 103
  • 173
Pnc_Gamba
  • 1
  • 1
  • Please edit your code to remove extra blank lines, and to indent it properly. – tgdavies Oct 15 '22 at 02:19
  • 2
    You have a semicolon at the end of your `else if ...` – tgdavies Oct 15 '22 at 02:22
  • To add to what @tgdavies said, look at `else if (onlyLettersAndDigitsTest(yolo, yolo)); printErrorMessage(2);` That `;` causes a [null statement](https://stackoverflow.com/questions/14112515/semicolon-at-end-of-if-statement). That makes execution of `printErrorMessage(2);` not dependent on the results of `lengthTest` or `onlyLettersAndDigitsTest`: It gets called every time a password is entered. – Old Dog Programmer Oct 15 '22 at 03:00
  • Is there a missing `!` in `if (Character.isLetterOrDigit(digandlet.charAt(i))) ` ? – Old Dog Programmer Oct 15 '22 at 03:03
  • Your `case 2` is missing a `break`. – QBrute Oct 15 '22 at 11:21
  • The description the O/P gave isn't quite right. The way I see the code, it always prints "Password does not meet requirement: must contain only letters and digits" followed by the similar "Password does not meet requirement: must contain at least 1 digit and not more than 3". This is a result of the combination of missing `break` for `case 2` and the null statement following `if (onlyLettersAndDigitsTest(yolo, yolo))`. If it fails `lengthTest`, 3 errors will be displayed. – Old Dog Programmer Oct 15 '22 at 14:56

1 Answers1

0

I recommend these changes be made in your code so that your code can work as it should.

  1. Change the if conditions in this way

    if (!lengthTest(yolo)) {
       printErrorMessage(1);
    }
    if (!onlyLettersAndDigitsTest(yolo)) {
       printErrorMessage(2); 
    }
    
  2. The onlyLettersAndDigitsTest method changed to this

    public static boolean onlyLettersAndDigitsTest(String password) {
        for (int i = 0; i < password.length(); i++) {
            if (!Character.isLetterOrDigit(password.charAt(i))) 
               return false;           
        }
        return true;
    }
    
  3. In the switch case, in the second case add the break

    public static void printErrorMessage(int errorCode) {
    switch (errorCode) {
        case 1:
            System.out.print("Password does not meet the requirement: must be between 6 and 15 characters");
            break;
        case 2:
            System.out.print("Password does not meet the requirement: must contain only letters and digits");
            break;
        case 3:
            System.out.print("Password does not meet the requirement: must contain at least 1 digit and not more than 3");
            break;
    
        case 4:
            System.out.print("Password does not meet the requirement: must be different than previous 2 passwords");
            break;
    }
    }
    
Uran Lajci
  • 61
  • 4