Question
Leap year Java problem. My question is to create a program that tests the user to imput valid dates taking into account leap years. I
Leap year Java problem. My question is to create a program that tests the user to imput valid dates taking into account leap years. I already have most of my code done. However, the leap years keep showing errors when they are actually valid dates and I cannot figure out why.
*** I know the problem is probably something simple like braces, "||", or "&&"
* for example 02/29/1968 is a valid date but the error is "Feb of that year has 28 days"
*** here is my code
/* * */
import java.util.Scanner;
public class HOMEWOKK4 { public static void main(String[] args) { Scanner console = new Scanner(System.in); for (int i = 1; i <= 15; i++) { System.out.println(" Please enter a valid data of the form mm/dd/yyyy."); String dateString = console.next(); checkDate(dateString); } } public static void checkDate(String dateString) { String savedDate = dateString; String monthString = dateString.substring(0, dateString.indexOf("/")); dateString = dateString.substring(dateString.indexOf("/") + 1); String dayString = dateString.substring(0, dateString.indexOf("/")); String yearString = dateString.substring(dateString.indexOf("/") + 1, dateString.length()); if( monthString.length() != 2 ) { System.out.println("Error with " + savedDate + ": The month must have two digits."); return; } if( dayString.length() != 2 ) { System.out.println("Error with " + savedDate + ": The day must have two digits."); return; } if( yearString.length() != 4 ) { System.out.println("Error with " + savedDate + ": The year must have four digits."); return; } int month = Integer.parseInt( monthString ); int day = Integer.parseInt( dayString ); int year = Integer.parseInt( yearString ); if(day < 1) { System.out.println("Error with " + savedDate + ": The day must be positive."); return; } if(year < 1) { System.out.println("Error with " + savedDate + ": The year must be positive."); return; } if(month > 12 || month < 1) { System.out.println("Error with " + savedDate + ": The month must be between 1 and 12."); return; } if(month == 4 || month == 6 || month == 9 || month == 11) { if(day < 1 || day > 30) { System.out.println("Error with " + savedDate + ": That month must have between 1 and 30 days."); return; } } if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if(day < 1 || day > 31) { System.out.println("Error with " + savedDate + ": That month must have between 1 and 31 days."); return; } } if(month == 2) { if((year % 400 == 0) || ((year % 4 == 0) && (year % 400 != 0))) { if(day < 1 || day > 29) { System.out.println("Error with " + savedDate + ": February of that year has 29 days."); return; } } } if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { if(day < 1 || day > 28) { System.out.println("Error with " + savedDate + ": February of that year has 28 days."); return; } } System.out.println("The date you entered, " + savedDate + ", is a valid date."); return; } }
*** here is the test program that it is supposed to satisfy
02/29/1900 Please enter a valid date of the form mm/dd/yyyy. Error with 1/12/2010: The month must have two digits.
Please enter a valid date of the form mm/dd/yyyy. Error with 07/4/2011: The day must have two digits.
Please enter a valid date of the form mm/dd/yyyy. Error with 15/01/1900: The month must be between 1 and 12.
Please enter a valid date of the form mm/dd/yyyy. Error with 04/-1/1900: The day must be positive.
Please enter a valid date of the form mm/dd/yyyy. Error with 04/001/1900: The day must have two digits.
Please enter a valid date of the form mm/dd/yyyy. Error with 11/12/99: The year must have four digits.
Please enter a valid date of the form mm/dd/yyyy. Error with 01/32/2015: That month must have between 1 and 31 days.
Please enter a valid date of the form mm/dd/yyyy. Error with 04/31/2015: That month must have between 1 and 30 days.
Please enter a valid date of the form mm/dd/yyyy. Error with 02/29/2015: February of that year has 28 days.
Please enter a valid date of the form mm/dd/yyyy. Error with 02/30/2016: February of that year has 29 days.
Please enter a valid date of the form mm/dd/yyyy. Error with 02/29/1900: February of that year has 28 days.
Please enter a valid date of the form mm/dd/yyyy. Error with 02/30/2000: February of that year has 29 days.
Please enter a valid date of the form mm/dd/yyyy. The date you entered, 12/31/2015, is a valid date.
Please enter a valid date of the form mm/dd/yyyy. The date you entered, 02/29/1968, is a valid date.
Please enter a valid date of the form mm/dd/yyyy. The date you entered, 08/31/9999, is a valid date.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started