/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package lab3part4switch; import java.util.Scanner; /** * * @author jcmtiernan */ public class Lab3Part4switch { /** * @param args the command line arguments */ public static void main(String[] args) { String monthName; String dayNum; String yearNum; String sp = " "; int day; int month; int year; boolean valid = true; Scanner in = new Scanner(System.in); System.out.println("Please enter a date in the form of month name then day number comma year number"); System.out.println("seperating the month and day by a space and putting a space after the comma."); System.out.println("Use this format Mmmmmmm dd, yyyy using one or two digits"+ " for day and two or four digits for year"); System.out.print("Please enter your date: "); monthName = in.next(); dayNum = in.next(); yearNum = in.next(); System.out.println("You entered "+monthName+sp+dayNum+sp+yearNum); // month name to integer value month = getMonthInt(monthName); // calling the method if (month == -1) { valid = false; } System.out.println("After call to getMonthInt, month is "+month); char first = dayNum.charAt(0); boolean isDayNum = ((( ((first == '0')||(first == '1')) || ((first == '2')||(first == '3')) ) || ( ((first == '4')||(first == '5')) || ((first == '6')||(first == '7')) )) || ((first == '8')||(first == '9')) ); if ((isDayNum) && (dayNum.length() == 3)) { // This turns the first two digits of string dayNum into an int day = Integer.parseInt(dayNum.substring(0,2)); } else if ((isDayNum) && (dayNum.length() == 2)) { // This turns the first two digits of string dayNum into an int day = Integer.parseInt(dayNum.substring(0,1)); } else { day = -1; valid = false; } first = yearNum.charAt(0); boolean isYearNum = ((( ((first == '0')||(first == '1')) || ((first == '2')||(first == '3')) ) || ( ((first == '4')||(first == '5')) || ((first == '6')||(first == '7')) )) || ((first == '8')||(first == '9')) ); if (isYearNum) { year = Integer.parseInt(yearNum); // This turns the string dayNum into an int } else { year = 999999; valid = false; } if (year < 100) { year += 2000; } if(valid) { valid = validateDay(day, month, year); } /* * Copy the code from part 3 year check here */ if (valid) { System.out.print("Your date is "+month+"/"+day+"/"+year+"\n"); } else { System.out.println("Your date is invalid"); } } // main method public static int getMonthInt(String monthNm) { int month; switch (monthNm) { case "January" : case "january": case "Jan.": month = 1; break; case "February" : case "february": case "Feb.": month = 2; break; case "March" : case "march" : case "Mar." : month = 3; break; case "April" : case "april" : case "Apr." : month = 4; break; case "May" : case "may" : month = 5; break; case "June" : case "june" : case "Jun." : month = 6; break; case "July" : case "july" : case "Jul." : month = 7; break; case "August" : case "august" : case "Aug." : month = 8; break; case "September" : case "september" : case "Sept." : month = 9; break; case "October" : case "october" : case "Oct." : month = 10; break; case "November" : case "november" : case "Nov." : month = 11; break; case "December" : case "december" : case "Dec." : month = 12; break; default : month = -1; } return month; } // getMonthInt public static boolean validateDay(int day, int month, int year) { boolean valid = true; if (day > 0) { if (( ((month == 1) || (month == 3 )) || ((month == 5) || (month == 7)) ) || ( ((month == 8) || (month == 10)) || (month == 12)) ) { if (day > 31) { valid = false; } } else if (( ((month == 4) || (month == 6 )) || ((month == 9) || (month == 11)) )) { if (day > 30) { valid = false; } } else if (month == 2) { if (isLeapYear(year)) { if(day > 29) { valid = false; } } else if (day > 28) { valid = false; } } } else { valid = false; } return valid; } private static boolean isLeapYear(int year) { boolean ly = false; if ((year % 4) == 0) { if ((year % 100) != 0) { ly = true; } else if ((year % 400) == 0) { ly = true; } } return ly; } }