Date Validation with Leap Years

getting leap years, and days in month to work

Page 1 of 1

9 Replies - 13093 Views - Last Post: 14 March 2010 - 10:33 PM Rate Topic: -----

#1 crossfusion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-March 10

Date Validation with Leap Years

Posted 14 March 2010 - 01:05 PM

I'm doing a date validation program in my Java class, and well it's pretty hard (for me that is). I have to be able to type in a date, have it say whether it's a leap year or not, and print out the number of days in the month. It seems pretty straight forward, but I get confused on trying to do the 'if else' statements and even the simplest things like getting the day prompting to work. :stupid: The years I'm doing only goes through 1000 to 1999, so that's why those numbers are there. The program isn't complete, so if anyone could help show me what I'm doing wrong in the areas I'm working on then I'd appreciate it...and I'm still kind of in the basics of Java so if you do hint me with some code then I'd appreciate it if it was stuff that's not too advanced so yea.

// ****************************************************************
// Dates.java
//
// Determine whether a 2nd-millenium date entered by the user
// is valid 
// ****************************************************************
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.print("Type in the month: " );
		month = scan.nextInt();
System.out.print("Type in the day: " );
		day = scan.nextInt();
System.out.print("Type in the year: " );
		year = scan.nextInt();
	
//Check to see if month is valid

if (month >= 1)
month = month;
else

if (month <= 12)
month = month;
else;


//Check to see if year is valid

if (year >= 1000)
year = year;
else

if (year <= 1999)
year = year;
else;

//Determine whether it's a leap year

//Determine number of days in month

if (year == 1 || 3 || 5 || 7 || 8 || 10 || 12)
	System.out.println (Number of days in month is 31);
	
	else (year == 4 || 6 || 9 || 11)
	System.out.println (Number of days in month is 30);
	

//User number of days in month to check to see if day is valid


//Determine whether date is valid and print appropriate message


}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Date Validation with Leap Years

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Date Validation with Leap Years

Posted 14 March 2010 - 01:23 PM

At this point I strongly recommend you make a function called something like "isLeapYear" where you will pass in the year as a parameter and will return a boolean (true it is a leap year or false it is not). I recommend this because it lends itself beautifully to a simple function which will contain the formula for figuring out the leap year and will keep things self contained within that function and not over complicate anything in your main function.

This function will contain the formula for figuring out a leap year. You can find this set of rules on the net...

How to determine whether a year is a leap year
To determine whether a year is a leap year, follow these steps:

1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
4. The year is a leap year (it has 366 days).
5. The year is not a leap year (it has 365 days).

All these checks can be done with a few lines of code within your new isLeapYear function. You could then make a separate function for returning how many days are in a given month based on the month and year. Do the leap year check function first and then you can call it from your other function for determining the number of days.

public boolean isLeapYear(int year) {
    // Apply rules listed above here.
    // Return true or false at the end.
}



Edit...

Btw, you could clean up your if statements a bit with something like...

if ((month < 1) || (month > 12)) {
   // Is not valid, don't let them proceed until it is correct.
}

// Otherwise they are just let through. No need to do month = month, that makes no sense. 




Hope this helps you out! :)

This post has been edited by Martyr2: 14 March 2010 - 01:31 PM
Reason for edit:: Added clarification for if statement

Was This Post Helpful? 0
  • +
  • -

#3 crossfusion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-March 10

Re: Date Validation with Leap Years

Posted 14 March 2010 - 02:07 PM

thanks for replying so um i feel even more confused...i tried to do the leapyear thing
public boolean leapYear (int year) {

if ((year % 4) == 0)
   {
	else ((year % 100) == 0)
	
	else ((year % 400) == 0)
	
	return = (year (false));
	}
return = (year (true));
}




but i keep getting errors galore...like illegal start of expression for public boolean and i'm not sure if i did the if else thing right :online2long: also at the beginning of the original code, i can't get it to prompt the user correctly without all three of the "type in the day, month, and year" expressions coming out at once Dx
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Date Validation with Leap Years

Posted 14 March 2010 - 02:16 PM

@Martyr2: Steps 2-3 don't take into account years like 1004 or 1996. Instead, you just need to check three conditions: year%4 == 0, and the OP's preconditions: year > 999 && year < 2000.

@Crossfusion: The whole point of else statements is so that you don't have to check conditions. Using a blanket else assumes all the above conditionals are false. If you do want to check a condition if the above conditional is false, you should use an else if statement. Like so:
if(condition){
   ..code..
}
else if(condition){
  ..code..
}



Note that an else or else if statement should not be nested inside the if statement.

Also, the following statmenet return = (year (false)); is illegal for a few reasons. First, year is a variable (the param in the method to be specific), so you cannot invoke year(boolean), as it isn't a method. Remember- methods have parentheses and variables do not. In addition, return is a reserved word in the Java language (and most languages), so you cannot assign it a value. Instead, you should either say return true; or return false;. This terminates the method, returns control to the spot where it was invoked, and sends the value or reference following it along.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Date Validation with Leap Years

Posted 14 March 2010 - 08:11 PM

What about ?
public boolean leapYear (int year) { 
  if(year % 4 != 0)
    return false;
  if(year % 400 == 0)
    return true;
  if(year % 100)
    return false;
  return true;
}


Was This Post Helpful? 0
  • +
  • -

#6 crossfusion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-March 10

Re: Date Validation with Leap Years

Posted 14 March 2010 - 09:04 PM

i don't know. i honestly just want my Java class to end already, and then i'm going to change majors. i just don't care about learning the language to be honest. i'm just cramming as much as i can into my head and hoping that the coding will work. the way my teacher explains it, makes sense, but there's just so much stuff, and i know i'm not cut out for it. ANYWAYS, my coding hasn't changed much, and i just get a lot of errors out of this:
// ****************************************************************
// Dates.java
//
// Determine whether a 2nd-millenium date entered by the user
// is valid 
// ****************************************************************
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.print("Type in the month: " );
		month = scan.nextInt();
System.out.print("Type in the day: " );
		day = scan.nextInt();
System.out.print("Type in the year: " );
		year = scan.nextInt();
	
//Check to see if month is valid

if ((month < 1) || (month > 12)){
}

//Check to see if year is valid

if (year >= 1000)
year = year;
else

if (year <= 1999)
year = year;
else;

//Determine whether it's a leap year

public boolean leapYear (int year)
 {
   if(year % 4 != 0)
 		 return false;
   if(year % 400 == 0)
		 return true;
	if(year % 100)
		 return false;
		 return true; 


}

//Determine number of days in month

if (year == 1 || 3 || 5 || 7 || 8 || 10 || 12)
	System.out.println (Number of days in month is 31);
	
	else (year == 4 || 6 || 9 || 11)
	System.out.println (Number of days in month is 30);
	

//User number of days in month to check to see if day is valid


//Determine whether date is valid and print appropriate message


}
}



but thanks everyone for being nice and helpful
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Date Validation with Leap Years

Posted 14 March 2010 - 09:08 PM

if (year == 1 || 3 || 5 || 7 || 8 || 10 || 12)        
else (year == 4 || 6 || 9 || 11)



Is invalid syntax. You have to explicitly give each boolean condition:
eg:
if (year == 1 || year == 3 || year==5 || year==7 ...)



This will work, but to make it less typing and more robust, you could use a Map.
http://java.sun.com/...rfaces/map.html
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Date Validation with Leap Years

Posted 14 March 2010 - 09:09 PM

View Postcrossfusion, on 14 March 2010 - 10:04 PM, said:

i don't know.

So waht is your algorithm to determine if a year is leap ?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Date Validation with Leap Years

Posted 14 March 2010 - 10:14 PM

View Postpbl, on 14 March 2010 - 11:11 PM, said:

  if(year % 100)
    return false;



It looks like you might be missing a comparison for year % 100. :)


@crossfusion: In this first if statement, you don't have any code inside of it, meaning that nothing will happen if you have an invalid month.
if ((month < 1) || (month > 12)){ 
} 



In this segment, you can shorten your code a little by saying if(year >= 1000 && year <= 1999){..code..}, rather than using two conditionals. Also, what is the purpose of else;? The whole reason for else is to execute code when all the above if statements evaluate to false. By putting a semi-colon there, absolutely nothing will happen. You might as well not even have the else; included.
//Check to see if year is valid 
 
if (year >= 1000) 
year = year; 
else if (year <= 1999) 
year = year; 
else; 



Also, at these two lines, you need to enclose the text in the parentheses in double quotes to signify that they are Strings, not variables. Otherwise, you will confuse Java and it will give you lots of errors, as the most of the words in the text are not variables, and even if they were, this is illegal String concatenation.
System.out.println (Number of days in month is 31); 
System.out.println (Number of days in month is 30); 



By the way, I'm sorry you feel overwhelmed in this class. Keep in mind though, that programming isn't a skill you can memorize. It is about understanding the concepts of problem solving and algorithm development. If you have those down, the syntax will follow. And most of the tools you use regularly, you will become very familiar with, to the point of memorization. However, you will not gain this skillset by cramming, as you would for history. So in other words, the key here is to practice, practice, practice. Trust me when I say that none of us were expert programmers on our first tries.

@Dogstopper: From the looks of the assignment, this is an Intro class, not Data Structures, so the OP probably hasn't seen Maps yet; and from the looks of things, it would only serve to further overwhelm him. Best to help him with the basics before throwing out the more advanced tools.
Was This Post Helpful? 1
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Date Validation with Leap Years

Posted 14 March 2010 - 10:33 PM

Good post mac. Really hit everything that I missed. And you're right...not ready for maps yet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1