the year has to be between 1000 and 1900, inclusive.
It should print out if the date is valid, and if it is valid if it is a leap year or not
If the date is invalid it will just print that it is invalid
here's my code:
import java.util.Scanner;
public class Dates
{
public static void main(String[] args)
{
int month, day, year; //date read in from user
int daysInMonth; //number of days in month read in
boolean monthValid, yearValid, dayValid; //true if input from user is valid
boolean leapYear; //true if user's year is a leap year
Scanner scan = new Scanner(System.in);
//Get integer month, day, and year from user
System.out.println("Enter the month: ");
month = scan.nextInt();
System.out.println("Enter the day: ");
day = scan.nextInt();
System.out.println("Enter the year: ");
year = scan.nextInt();
//Check to see if month is valid
if (month <= 12 && month > 0)
monthValid = true;
else
monthValid = false;
//Check to see if year is valid
if (year <= 1999 && year >= 1000)
yearValid = true;
else
yearValid = false;
//Determine whether it's a leap year
if (year%4 == 0 && year%100 != 0 || year%400 == 0)
leapYear = true;
else
leapYear = false;
//Determine number of days in month
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
daysInMonth = 31;
else if (month == 4 || month == 6 || month == 9 || month == 11)
daysInMonth = 30;
if (month == 2 && leapYear == true)
daysInMonth = 29;
else
daysInMonth = 28;
//User number of days in month to check to see if day is valid
if (day < daysInMonth && day > 0 || day == daysInMonth)
dayValid = true;
else
dayValid = false;
//Determine whether date is valid and print appropriate message
if (dayValid == true && yearValid == true && monthValid == true )
{
System.out.println("Date is Valid.");
if (leapYear == true)
System.out.println("Date is a leap year.");
else
System.out.println("Date is not a leap year.");
}
else
System.out.println("Date is invalid.");
}
}
for some reason when i do a date like december 31, 1600...it says the date is invalid although it is valid....
i don't see where my error is.

New Topic/Question
Reply



MultiQuote





|