2

I know I can use the date formatter, but I'm trying to learn.

I am trying to take a date from Scanner in the format of mm/dd/yyyy. I am having troubles when I take an input for the month. For example, if I want January I press 01 and monthInt converts it to 1 instead of 01.

I am also having trouble with my day too. It keeps saying invalid day when I enter a valid date. Is my if statement messed up? If you need any more explanation just ask. Thank you for your time!

My code:

    System.out.println("Enter a date in the format mm/dd/yyyy");
    String mm;
    String dd;
    String yyyy;
    String January = null;
    String February = null;
    String March = null;
    String April = null;
    String May = null;
    String June = null;
    String July = null;
    String August = null;
    String September = null;
    String October = null;
    String November = null;
    String December = null;
    String month = null;
    String date = null;

    Scanner keyboard = new Scanner(System.in);

    date = keyboard.next();
    if (date.length() != 10) {
        System.out.println("Format of date is wrong");
    }
    if (date.charAt(2) != '/') {
        System.out.println("Must have / after mm");
    }
    if (date.charAt(5) != '/') {
        System.out.println("Must have / after dd");
    }

    else if (date.length() == 10 && date.charAt(2) != '/' && date.charAt(5) != '/') {
        System.out.println(date + " date is valid");
    }

    mm = date.substring(0, 1);
    int monthInt = Integer.parseInt(mm);        

    if (monthInt == 1) {
        month = January;
    } else if (monthInt == 2) {
        month = February;
    } else if (monthInt == 3) {
        month = March;
    } else if (monthInt == 4) {
        month = April;
    } else if (monthInt == 5) {
        month = May;
    } else if (monthInt == 6) {
        month = June;
    } else if (monthInt == 7) {
        month = July;
    } else if (monthInt == 8) {
        month = August;
    } else if (monthInt == 9) {
        month = September;
    } else if (monthInt == 10) {
        month = October;
    } else if (monthInt == 11) {
        month = November;
    } else if (monthInt == 12) {
        month = December;
    } else {
        System.out.println("Invalid Month");
    }

    if (monthInt < 0 || monthInt > 12) {
        System.out.println("Invalid month");
    }

    dd = date.substring(3, 4);
    int dayInt = Integer.parseInt(dd);

    if (monthInt == 1 || monthInt == 3 || monthInt == 5 || monthInt == 7 || monthInt == 8 || monthInt == 10
            || monthInt == 12 && dayInt < 0 && dayInt > 31) {
        System.out.println("Invalid day");
    }

    if (monthInt == 4 || monthInt == 6 || monthInt == 9 || monthInt == 11 
             && dayInt < 0 && dayInt > 30) {
        System.out.println("Invalid day");

    }
    if (monthInt == 2 && dayInt < 0 && dayInt > 28){
        System.out.println("Invalid day");
    }
Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
Stepsie94
  • 29
  • 3
  • Which days and for which months are u getting the error? – DarkV1 Jul 06 '16 at 17:58
  • Remove all of the `String monthName = null` stuff. It's clutter and not necessary. Instead, surround your `month = monthName` code with quotations, making it a real String (eg `month = "January"`) – Dylan Wheeler Jul 06 '16 at 18:00

2 Answers2

3

Your if statements on the invalid day checks are not correct. It should look like this:

if ((monthInt == 1 || monthInt == 3 || monthInt == 5 || monthInt == 7 || monthInt == 8 || monthInt == 10 || monthInt == 12) && (dayInt <= 0 || dayInt > 31)) {
    System.out.println("Invalid day");
}

if ((monthInt == 4 || monthInt == 6 || monthInt == 9 || monthInt == 11) && (dayInt <= 0 || dayInt > 30)) {
    System.out.println("Invalid day");
}

if (monthInt == 2 && (dayInt <= 0 || dayInt > 28)) {
    System.out.println("Invalid day");
}

Note that the dayInt < 0 statement was changed to dayInt <=0 because a day int of 0 is also invalid.

The key here is that you want it to be an invalid day if it is one of the given months, and it is outside the day range for that month. Therefore you use a compound if of the form if(month1 OR month2 etc) AND (outsiderange1 OR outsiderange2).

nhouser9
  • 6,730
  • 3
  • 21
  • 42
0

Looks like the problem is here:

mm = date.substring(0, 1);

It takes the first character from the date string, so if you enter '01' for January, it will take '0', so monthInt will be 0 and you will get "Invalid month" error. If you want to get two characters, you need mm = date.substring(0, 2);

The same problem exists with date, you need dd = date.substring(3, 5);

Peter L
  • 302
  • 1
  • 6