0

I'm not sure why the console scanner is not accepting month in two-digit form, like 10,11,12. Only accepts single digit.

I have same prompts, and boolean validation methods for year and day, and they work as expected.

private int promptForMonth() {

    String monthResponse = "";
    int month = -1;
    do {
        System.out.println("\t" + "Please enter the month number - "
                + "for example '6':");
        monthResponse = scan.next();
        if (validateMonthString(monthResponse)) {
            month = Integer.parseInt(monthResponse);
        }
    } while (month == -1);
    return month;
}

boolean validation below.

private boolean validateMonthString(String month) {
    boolean isValid = true;
    if (month.charAt(0) == '0') {
        isValid = false;
        return isValid;
    } else if (!(month.length() == 2)) {
        isValid = false;
        return isValid;
    }
    for (int x = 1; x < month.length(); x++) {
        if (!(month.charAt(x) >= 49 && month.charAt(x) <= 57)) {
            isValid = false;
            return isValid;
        }
    }
    if (Integer.parseInt(month) > 12) {
        isValid = false;
        return isValid;
    }
    return isValid;
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Nad Deb
  • 29
  • 4
  • @Pshemo have changed it. sorry. – Nad Deb Nov 13 '20 at 13:07
  • The second digit can only be 1 thru 9? What about October? – Kevin Anderson Nov 13 '20 at 13:25
  • Not really solution but `if (!(month.charAt(x) >= 49 && month.charAt(x) <= 57)) {` would be more readable as `if (!(month.charAt(x) >= '1' && month.charAt(x) <= '9')) {`. More on this subject: [What is a magic number, and why is it bad?](https://stackoverflow.com/q/47882) – Pshemo Nov 13 '20 at 13:30
  • @Pshemo it still won't accept 10, 11, 12. – Nad Deb Nov 13 '20 at 13:54
  • That is why I said "Not really solution". – Pshemo Nov 13 '20 at 15:18
  • Anyway if you are familiar with regex your code can be reduced to `private boolean validateMonthString(String month){return month.matches("[1-9]|1[0-2]");}` which means: `[1-9]` single character in range '1' to '9' OR `1[0-2]` which represents singe character '1' followed by second character in range '0' to '2' (meaning 10, 11, 12). – Pshemo Nov 13 '20 at 15:20

1 Answers1

1

There are 3 problems in your method:

  • if (!(month.length() == 2)) rejects any single-digit number, meaning your method will reject the numbers 1 - 9
  • Your loop does not check the first character, accepting negative numbers like -1
  • Your loop rejects 0, rejecting the valid number 10

Also isValid = false; return isValid; could easily be replaced by return false;

Fixing all this together results in the following code which accepts exactly the numbers 1 to 12.

private static boolean validateMonthString(String month) {
    if (month.charAt(0) == '0') {
        return false;
    }
    // not actually needed, but improves runtime
    if (month.length() > 2) {
        return false;
    }
    for (int x = 0; x < month.length(); x++) {
        if (!(month.charAt(x) >= '0' && month.charAt(x) <= '9')) {
            return false;
        }
    }
    if (Integer.parseInt(month) > 12) {
        return false;
    }
    return true;
}
MDK
  • 499
  • 3
  • 14