0

So I started the code below to make a date printer, I screwed up that code so I decided to try to change all of the if and else statements to switch statements. The code is suppose to take two integers and print out a date. For example typing in: 1 2 would print: January 2nd

I know the code I have below is not correct, but it will probably help explain what I am trying to do. (Sorry I am pretty new to this)

    class DatePrinter {
    public static void main(String[] args) {
    int month, day;
    boolean dateIsValid = false;

    System.out.println("Enter two integers.  Press return after each 
    integer.");
    month = A7Helper.nextInteger();
    day = A7Helper.nextInteger();

    if (month == 4 && day >= 1 && day <= 30) {
        System.out.print("April ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 5 && day >= 1 && day <= 31) {
        System.out.print("May ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 7 && day >= 1 && day <= 30) {
        System.out.print("June ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 7 && day >= 1 && day <= 31) {
        System.out.print("July ");
        System.out.print(day);
        dateIsValid = true;

    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 8 && day >= 1 && day <= 31) {
        System.out.print("August ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 9 && day >= 1 && day <= 31) {
        System.out.print("September ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 10 && day >= 1 && day <= 31) {
        System.out.print("October ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 11 && day >= 1 && day <= 30) {
        System.out.print("November ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 12 && day >= 1 && day <= 31) {
        System.out.print("December ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 1 && day >= 1 && day <= 31) {
        System.out.print("January");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 2 && day >= 1 && day <= 28) {
        System.out.print("February ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (month == 3 && day >= 1 && day <= 31) {
        System.out.print("March ");
        System.out.print(day);
        dateIsValid = true;
    } else {
        System.out.println("Invalid Date");
        dateIsValid = false;
    }

    if (day == 1) {
        System.out.println("th");
    } else {
        System.out.println("th");
    }

    if (day == 2) {
        System.out.println("nd");
    } else {
        System.out.println("th");
    }

    if (day == 3) {
        System.out.println("rd");
    } else {
        System.out.println("th");
    }
    }
    }

also the A7Helper

import java.util.Scanner;
public class A7Helper {
static Scanner keyboard = new Scanner(System.in);
static int nextInteger() {
return keyboard.nextInt();
}
}
JScannell
  • 13
  • 3

1 Answers1

0

Here's how to do it with a switch statement:

dateIsValid = true;
switch (month) {
    case 2:
        maxday = 28;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        maxday = 30;
        break;
    default:
        if (month > 12) {
            dateIsValid = False;
        }
        maxday = 31;
        break;
}
if (day < 1 || day > maxday) {
    dateIsValid = false;
}
if (!dateIsValid) {
    System.out.println("Invalid Date");
}

Note that the code is correct only for a non-leap year. Case 2 needs to be fixed accordingly.

This is fine for learning how to write code, but if you really need to handle dates in a real application, you should use a Java library intended for the purpose, because time always ends up being more complicated than one would think.

I see that I also didn't handle providing the name of the month. To do that, just make a separate case for every month, like this:

case 1:
    monthname = "Jan"
    maxday = 31
    break;

Definitions for monthname and maxday are left to the student as an exercise. ;-)

Jeff Learman
  • 2,914
  • 1
  • 22
  • 31
  • 1
    This doesn't check for month correctness. I'd add `case` statements for months 1,3,5,7,8,10,12 too, in which you set `maxday` to 31. This way, you can use the `default` case for `dateIsValid = false`. I'd also initialize `dateIsValid` to `true` before the `switch`, and just set it to false whenever I encounter an invalid month/day (without setting it to `true` again later) – BackSlash May 03 '18 at 17:14
  • @BackSlash: Good point! I'll fix it. – Jeff Learman May 03 '18 at 17:15