/* * 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 lab3part4spr16; import java.util.Scanner; /** * * @author jcmtiernan */ public class Lab3Part4Spr16 { /** * @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 if ((monthName.equalsIgnoreCase("January")) || (monthName.equalsIgnoreCase("jan")) || (monthName.equals("Jan."))) { month = 1; } else if ((monthName.equalsIgnoreCase("February")) || (monthName.equalsIgnoreCase("feb")) || (monthName.equals("Feb."))) { month = 2; } /* * Fill in the missing months */ else if ((monthName.equalsIgnoreCase("December")) || (monthName.equalsIgnoreCase("dec")) || (monthName.equals("Dec."))) { month = 12; } else { month = -1; valid = false; } 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 one digits of string dayNum into an int day = Integer.parseInt(dayNum.substring(0,1)); } else { day = -1; valid = false; } /* * Copy the code from part 3 day check here to check the number of days in month */ 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; } /* * 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"); } } }