/* * 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 lab3part3spr16; import java.util.Scanner; /** * * @author jcmtiernan */ public class Lab3Part3Spr16 { /** * @param args the command line arguments */ public static void main(String[] args) { int day, year, moNum; String month = new String(); Scanner in = new Scanner(System.in); String monthName = "MonthName"; String sp = " "; boolean valid = true; System.out.println("Please enter a valid date after the year 1600 in the following form. "); System.out.println("Enter a two digit integer for month MM (1 - 12) then a space "); System.out.println("Enter a two digit integer for day DD (1 - 31 max) then a space "); System.out.println("Enter a two digit integer for month MM (1 - 12) then a space "); System.out.println("and a three letter abbreviation for month mmm (\"Jan\"...\"Dec\") then a space "); System.out.println("and then a four digit integer for year YYYY (> 1600)."); System.out.print("Enter your DD mmm YYYY: "); day = in.nextInt(); month = in.next(); year = in.nextInt(); System.out.print("You entered "+month+" "+day+", "+year+"\n"); // Validate the month input and get an integer representing the month moNum = findMoNum(month); // Validate the day for the correct months // Ignore the leap/common year for February days, use max of 29 for Feburary // Jan, Mar, May, July, Aug, Oct, Dec have 31 days // Sept, April, June, Nov have 30 days if (day > 0) { if (( ((moNum == 1) || (moNum == 3 )) || ((moNum == 5) || (moNum == 7)) ) || ( ((moNum == 8) || (moNum == 10)) || (moNum == 12)) ) { if (day > 31) { valid = false; } } /* * Complete the valid date checking for months with 30 days and for February */ // else if } else { valid = false; } // Validate year as > 1600 and only 4 digits (so < 10000) /* Complete the test for the if statement to do the year validation */ //if ((year <= 1600) { //valid = false; } if (valid) { System.out.println("Your date is "+moNum+"/"+day+"/"+year); } else { System.out.println("Your date is invalid"); } } public static int findMoNum(String mo) { int numMo = 1; // default value for month will be January if invalid input is given if (mo.equalsIgnoreCase("Jan")) { numMo = 1; } else if (mo.equalsIgnoreCase("Feb")) { numMo = 2; } // write the code for the missing months else if (mo.equalsIgnoreCase("Dec")) { numMo = 12; } return numMo; } }