// This program illustrate the SWITCH statement; it validates // if a date is entered in the required format (MM/DD/YYYY), // and checks if it is a valid date. It also reports if the // year is a leap year. import java.util.Scanner; import java.util.StringTokenizer; class SwitchExample2 { public static void main(String[] args){ Scanner scan = new Scanner (System.in); StringTokenizer date; String month, day, year; int m, d, y; boolean error = false; boolean leapYear = false; System.out.print ("Input a date in the format MM/DD/YYYY "); date = new StringTokenizer(scan.nextLine(), "/"); // split up date into month, day and year month = date.nextToken(); day = date.nextToken(); year = date.nextToken(); // convert strings month, day and yeat to integers m = Integer.parseInt(month); d = Integer.parseInt(day); y = Integer.parseInt(year); switch (m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (d > 31 || d < 1) { System.out.println ("Wrong day value. Must be a number" + " between 1 and 31 for the specified month."); error = true; } break; case 4: case 6: case 9: case 11: if (d > 30 || d < 1) { System.out.println ("Wrong day value. Must be a number" + " between 1 and 30 for the specified month."); error = true; } break; case 2: if (y%4 == 0) leapYear = true; if ((leapYear) && ((d > 29) || (d < 1))) { System.out.println ("Wrong day value. Must be a number" + " between 1 and 29 for the specified month."); error = true; } else if ((!leapYear) && ((d > 28) || (d < 1))) { System.out.println ("Wrong day value. Must be a number" + " between 1 and 28 for the specified month."); error = true; } break; default: System.out.println ("Wrong month value. Must be a " + "number between 1 and 12"); error = true; } if (leapYear) System.out.println (y + " is a leap year"); if (!error) System.out.println ("The date is valid and is in the required format."); } }