[java] Checking for a valid date

Started by
10 comments, last by fadilthrejk 20 years, 5 months ago
I am workin on a program to check if a date inputted by the user is valid, and output whether it''s valid or not and whether it''s a leap year. I got it all working, except for the leap year thing. I''m not sure if I have the rules right. I think this is what it is: it''s a leap year if a) it''s divisible by 400, or b) it''s divisible by 4 and it''s not divisible by 100. i dunno. I am also not sure if i''ve translated the rules into java correctly or not. here''s my code for that section: double rem = month%400; double rem2 = month%100; double rem3 = month%4; if (year < 3000 && year > 1999) { yearValid = true; if ((rem == 0) || ((rem2 > 0) && (rem3 == 0))) //Determine whether it''s a leap year { leapYear = true; } //no need to tell it to make it false otherwise, because it''s initialized } //as false Ok, i think that''s right. here''s the full code for the program. I know it''s sloppy, but I just threw most of it together in like 30 minutes or so. import cs1.Keyboard; /** * Write a description of class Dates here. * * @author David Grimsley * @version 10-22-03 */ public class Dates { /** * Main...everything is done here (in this case)...only works like that in the console tho...applets don''t even have a main method * */ public static void main(String[] args) { int month, days, year; //date read in from user int daysInMonth; //number of days in month read in boolean monthValid = true; boolean yearValid = true; boolean dayValid = true; //true if input from user is valid boolean leapYear = false; //true if user''s year is a leap year //Get integer month, day, and year from user System.out.println("Enter the month number"); month = Keyboard.readInt(); System.out.println("Enter the day of the month"); days = Keyboard.readInt(); System.out.println("Enter the year"); year = Keyboard.readInt(); //Check to see if month is valid if (month <= 12 && month >= 1) monthValid = true; else monthValid = false; int rem = month%400; int rem2 = month%100; int rem3 = month%4; if (year < 3000 && year > 1999) { yearValid = true; if ((rem == 0) || ((rem2 > 0) && (rem3 == 0))) //Determine whether it''s a leap year { leapYear = true; } //no need to tell it to make it false otherwise, because it''s initialized } //as false else yearValid = false; //Determine number of days in month int monthDays; //User number of days in month to check to see if day is valid //Probably a bit more complex than necessary, I may fix eventually if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { monthDays = 31; if (days > 31) dayValid = false; } else{ if(leapYear && month == 2) monthDays = 29; if (days > 29) dayValid = false; else if(month == 4 || month == 6 || month == 9 || month == 11) { monthDays = 30; if (days > 30) dayValid = false; } else { monthDays = 28; if (days > 28) { dayValid = false; } } } //Determine whether date is valid and print appropriate message if (monthValid == true && yearValid == true && dayValid == true) { System.out.println("Date is valid"); if (leapYear) System.out.println("Leap year"); } else System.out.println("Date is not valid"); } //end main() } //end class Dates if anyone can help me, thanks. if not, i''ll figure it out on my own eventually.
Advertisement
Why are you checking for leap years with month? It''s the year that determines it.
i''m not. this is where it''s checked:


double rem = month%400;
double rem2 = month%100;
double rem3 = month%4;
if ((rem == 0) || ((rem2 > 0) && (rem3 == 0))) //Determine whether it''s a leap year
{
leapYear = true;

but it tells me when i put in a leap year the year isn''t valid at all. I was wondering if i screwed up in the algorithm or something. i still don''t know why it''s doing it.
yeah, and you''re using month. month is between 1 and 12, it''s not the year. the year is what determines a leap year.

Just pick a value for month, say 5 for May. Run 5 through that little snippet of code manually, and see what you get. It shouldn''t make any sense.

Then try running 1996 though that snippet manually.
Are you doing this just as like a practice challenge? Or are you doing it because it actually needs to be done? Because, if it''s not just as a challenge, turn to the Java API, there is a class GregorianCalender that stores dates (the class Date has been deprecated) and it has a method isLeapYear(int year) that returns a boolean value.

Java provides a lot of useful classes for you, always check the API if you''re having trouble.
[url = "http://geekmangames.com"]GeekMan Games[url]
You can also check the source for that class to see how they do it, too.
[url = "http://geekmangames.com"]GeekMan Games[url]
I think I fixed it. I''ve tested a wide range of months, days, and years. They all work. I could have used the GregorianCalendar class, had I known about it, but I figure since I''ve already done it and it works fine for what I need it for, I''ll just leave it as is. Thanks for pointing out about the leap year validation. I don''t know what I was thinking putting month to test instead of date. Like I said, I threw it together in like 35 minutes. Here''s the code, finished (hopefully):

import cs1.Keyboard;
/**
* Write a description of class Dates here.
*
* @author David Grimsley
* @version 10-22-03
*/
public class Dates
{
/**
* Main...everything is done here (in this case)...only works like that in the console tho...applets don''t even have a main method
*
*/
public static void main(String[] args)
{
int month, days, year; //date read in from user
int daysInMonth; //number of days in month read in
boolean monthValid = true;
boolean yearValid = false;
boolean dayValid = false; //true if input from user is valid
boolean leapYear = false; //true if user''s year is a leap year

//Get integer month, day, and year from user
System.out.println("Enter the month number");
month = Keyboard.readInt();

System.out.println("Enter the day of the month");
days = Keyboard.readInt();

System.out.println("Enter the year");
year = Keyboard.readInt();

//Check to see if month is valid
if (month <= 12 && month >= 1)
monthValid = true;

else monthValid = false;

int rem = year%400;
int rem2 = year%100;
int rem3 = year%4;

if (year < 3000 && year > 1999)
{
yearValid = true;

if (yearValid && ((rem == 0) || ((rem2 > 0) && (rem3 == 0)))) //Determine whether it''s a leap year
{
leapYear = true;
}

}
else yearValid = false;

//User number of days in month to check to see if day is valid
//Probably a bit more complex than necessary, I may fix eventually
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (days <= 31)
dayValid = true;
}
else{
if (month == 2)
{
if (leapYear && days <= 29)
dayValid = true;
else
if (days <= 28)
dayValid = true;
}
else
if(month == 4 || month == 6 || month == 9 || month == 11)
{
if (days <= 30)
dayValid = true;
}
}



//Determine whether date is valid and print appropriate message
if (monthValid == true && yearValid == true && dayValid == true)
{
System.out.println("Date is valid");
if (leapYear)
System.out.println("Leap year");
}
else System.out.println("Date is not valid");

} //end main()

} //end class Dates


thanks for all the help, especially tortoise for pointing out my stupid error with the leapYear checker.

IrateGoldfish: Where can I view the source of the class? I thought the Java sdk just came with the class files.
You can see all the source files by extracting an archive called source.zip that is located in the directory where you installed the java sdk....
garazdawi - 'I put the laughter back in slaughter'
Wow! I never noticed that before.
Isn''t this a class assignment? What would your teacher say to a 3 line program that uses GregorianCalendar?

This topic is closed to new replies.

Advertisement