date validation

using a method to validate a given date

Page 1 of 1

13 Replies - 2895 Views - Last Post: 31 August 2009 - 08:45 PM Rate Topic: -----

#1 hidden_within  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-April 09

date validation

Posted 27 August 2009 - 07:06 AM

I am trying to validate a date using a method from the DateRec but every day I enter returns false. Any suggestions?

public class DateRec
{
	public int month;
	public int day;
	public int year;
	public boolean good;
	
	public DateRec()
	{
		month = 1;
		day = 1;
		year = 2006;
		good = true;
	}
	
	public DateRec(int month, int day, int year)
	{
		month = month;
		day = day;
		year = year;
		
		
	}
	public void validate()
	{
		if(day <= 0)
			good = false;
		
		
		else if((month == 1) && (day <= 31))
			good = true;
		else if(month == 2)
		{		
			boolean isleap = true;
				if(year % 4 != 0)
					isleap = false;
				else if((year % 100 == 0) && (year % 400 == 0))
					isleap = true;
			if((true) && (day <= 29))
				good = true;
			else if((false) && (day <= 28))
				good = true;
		}
	
		else if((month == 3) && (day <= 31))
			good = true;
		else if((month == 4) && (day <= 30))
			good = true;
		else if((month == 5) && (day <= 31))
			good = true;
		else if((month == 6) && (day <= 30))
			good = true;
		else if((month == 7) && (day <= 31))
			good = true;
		else if((month == 8) && (day <= 31))
			good = true;
		else if((month == 9) && (day <= 30))
			good = true;
		else if((month == 10) && (day <= 31))
			good = true;
		else if((month == 11) && (day <= 30))
			good = true;
		else if((month == 12) && (day <= 31))
			good = true;
		else
			good = false;
		
	}
	public boolean getDate()
	{
		return good;
	}

}//end class


public class dateRecTest
{
	public static void main(String[] args)
	{
	DateRec date = new DateRec();
	DateRec date2 = new DateRec(1, 30, 2000);
		
		date.validate();
		date2.validate();
		
		System.out.println(date.getDate());
		System.out.println(date2.getDate());

			}
}


Is This A Good Question/Topic? 0
  • +

Replies To: date validation

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: date validation

Posted 27 August 2009 - 07:27 AM

You should use just one function to determine if a date is valid, hence instead of having the function public boolean getDate() just have a function public boolean validate() and in the positions you set good = true; or good = false; simply return eg. return true; or return false;

As for why it is always returning false, that would be an issue with your if and if else statements, revise the statements and try simplifying it. Sometimes look at the other side of the coin and it may simplify your problem.

This post has been edited by bbq: 27 August 2009 - 07:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: date validation

Posted 27 August 2009 - 07:58 AM

I had to add the "this" words to get it to work.

	
public DateRec(int month, int day, int year)
	{
		this.month = month;
		this.day = day;
		this.year = year;
	}


Was This Post Helpful? 0
  • +
  • -

#4 hidden_within  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-April 09

Re: date validation

Posted 27 August 2009 - 08:05 AM

I would use a value-returning method for the validate() method but my assignment calls for a void method. I am supposed to use the validate() method to instantiate the instance variable boolean good;

The assignment says to call the method validate() within the constructor (non-default). How would I go about doing that.



 
public class DateRec
{
	public int month;
	public int day;
	public int year;
	public boolean good;
	
	public DateRec()
	{
		month = 1;
		day = 1;
		year = 2006;
		good = true;
	}
	
	public DateRec(int month, int day, int year)
	{
		this.month = month;
		this.day = day;
		this.year = year;
		this.good = validate();
		
		
	}
	private void validate()
	{
		if((month == 1) && (day <= 31))
			good = true;
	   	
		 		
	}
	

}//end class



I know that I cant use this.good = validate(); because it says that they are incompatible type but how would I set the value for the variable good using the validate method?
Was This Post Helpful? 0
  • +
  • -

#5 Laythe  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 76
  • Joined: 09-June 09

Re: date validation

Posted 27 August 2009 - 02:41 PM

ok, so tell me what is year divided on 4 or 100 or 400 stands for, what is that mean so i may can help ye ;)
Was This Post Helpful? 0
  • +
  • -

#6 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: date validation

Posted 27 August 2009 - 02:58 PM

View Posthidden_within, on 27 Aug, 2009 - 10:05 AM, said:

I know that I cant use this.good = validate(); because it says that they are incompatible type but how would I set the value for the variable good using the validate method?


I got 2 true's when I changed those fields to "this" and ran the program. I don't have this in your DateRec function though, because it doesn't return anything (void).
this.good = validate();



But mine works and I cut and pasted your code and just added the this.'s. ... :blink:


View PostLaythe, on 27 Aug, 2009 - 04:41 PM, said:

ok, so tell me what is year divided on 4 or 100 or 400 stands for, what is that mean so i may can help ye ;)

That is how you tell if any given year is a "leap year" or not. The year 2000 wasn't suppose to be a leap year, but it happened to be divisible by 400, so we had a Feb. 29th that year!
Was This Post Helpful? 0
  • +
  • -

#7 hidden_within  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-April 09

Re: date validation

Posted 27 August 2009 - 07:38 PM

View PostNoobKnight, on 27 Aug, 2009 - 01:58 PM, said:

View Posthidden_within, on 27 Aug, 2009 - 10:05 AM, said:

I know that I cant use this.good = validate(); because it says that they are incompatible type but how would I set the value for the variable good using the validate method?


I got 2 true's when I changed those fields to "this" and ran the program. I don't have this in your DateRec function though, because it doesn't return anything (void).
this.good = validate();



But mine works and I cut and pasted your code and just added the this.'s. ... :blink:


View PostLaythe, on 27 Aug, 2009 - 04:41 PM, said:

ok, so tell me what is year divided on 4 or 100 or 400 stands for, what is that mean so i may can help ye ;)

That is how you tell if any given year is a "leap year" or not. The year 2000 wasn't suppose to be a leap year, but it happened to be divisible by 400, so we had a Feb. 29th that year!


I wasn't able to figure out how to validate the date using the method validate() and instantiate the variable good but I just decided to use a value-returning method to instead. It's not what my assignment calls for but it works.

I am having trouble with validating the month of February due to the leap year statments. I know that my if-else statements work correctly to return true or false when finding if a year is a leap year or not but my code accepts values for Feb. that shouldn't be valid. For instance, 2/29/2000 should return true and set the value for the variable good to true but the date 2/29/2001 should return false because that year is not a leap year, therefore there are only 28 days in Feb not 29. Any suggestions?

public class DateRec
{
	public int month;
	public int day;
	public int year;
	public boolean good;
   
	public DateRec()
	{
		month = 1;
		day = 1;
		year = 2006;
		good = true;
	}
   
	public DateRec(int month, int day, int year)
	{
		this.month = month;
		this.day = day;
		this.year = year;
		  good = validate();
	   
	   
	}
	public boolean validate()
	{
	 
	 		if(day <= 0)
			good = false;
	   
	   
		else
		 if((month == 1) && (day <= 31))
			good = true;
		else if(month == 2)
		{		
			boolean isleap = true;
				if(year % 4 != 0)
					isleap = false;
				else if((year % 100 == 0) && (year % 400 == 0))
					isleap = true;
				 if((true) && (day <= 29))
					 	good = true;
				else if((false) && (day <= 28))
						good = true;
		}
   
		else if((month == 3) && (day <= 31))
			good = true;
		else if((month == 4) && (day <= 30))
			good = true;
		else if((month == 5) && (day <= 31))
			good = true;
		else if((month == 6) && (day <= 30))
			good = true;
		else if((month == 7) && (day <= 31))
			good = true;
		else if((month == 8) && (day <= 31))
			good = true;
		else if((month == 9) && (day <= 30))
			good = true;
		else if((month == 10) && (day <= 31))
			good = true;
		else if((month == 11) && (day <= 30))
			good = true;
		else if((month == 12) && (day <= 31))
			good = true;
	   
	   return good;
	}
	public boolean getDate()
	{
		return good;
	}

}//end class

public class dateRecTest
{
	public static void main(String[] args)
	{
	DateRec date = new DateRec();
	DateRec date2 = new DateRec(2,29,2001);
		
	
		
		System.out.println(date.getDate());
		System.out.println(date2.getDate());

			}
}

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

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

Re: date validation

Posted 27 August 2009 - 08:08 PM

View PostNoobKnight, on 27 Aug, 2009 - 06:58 AM, said:

Does anybody else miss Cobol? :(

Don't really miss the language but miss the $200K/year it gave me for a few years :)
I'll go back for a Cobol contract any time
Was This Post Helpful? 0
  • +
  • -

#9 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: date validation

Posted 27 August 2009 - 09:49 PM

				if((true) && (day <= 29))
						 good = true;
				else if((false) && (day <= 28))
						good = true;


Don't use the (true) and (false) explicitly.
Use (isleap==true)
and (isleap==false) instead.
Was This Post Helpful? 0
  • +
  • -

#10 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: date validation

Posted 27 August 2009 - 09:55 PM

View Postpbl, on 27 Aug, 2009 - 10:08 PM, said:

View PostNoobKnight, on 27 Aug, 2009 - 06:58 AM, said:

Does anybody else miss Cobol? :(

Don't really miss the language but miss the $200K/year it gave me for a few years :)
I'll go back for a Cobol contract any time



We're dumping Cobol and learning Java, I think Cobol was simpler, but I will learn to love Java :wub:, hopefully sooner than later.
Was This Post Helpful? 0
  • +
  • -

#11 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: date validation

Posted 27 August 2009 - 09:57 PM

Quote

				if((true) && (day <= 29))
						 good = true;
				else if((false) && (day <= 28))
						good = true;


Overly complicated code of the day goes to this one, which will translate to
				if(day <= 29)
						 good = true;
				else if(false)
						good = true



And then down to
good = (day<= 29);



Though my guess was that the original was just a typo and the author left out the variable isleap

This post has been edited by syfran: 27 August 2009 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

#12 hidden_within  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-April 09

Re: date validation

Posted 28 August 2009 - 07:11 PM

Thanks guys I was able to get the code to validate the users date.

 public boolean validate()
	{
	 
	 		if(day <= 0)
			good = false;
	   
	   
		else if((month == 1) && (day <= 31))
			good = true;
		else if(month == 2)
		{		
			boolean isleap = true;
				if(year % 4 != 0)
					isleap = false;
				else if((year % 100 == 0) && (year % 400 == 0))
					isleap = true;
				 if((isleap == true) && (day <= 29))
					 	good = true;
				else if((isleap == false) && (day <= 28))
						good = true;
		}//end if
   
		else if((month == 3) && (day <= 31))
			good = true;
		else if((month == 4) && (day <= 30))
			good = true;
		else if((month == 5) && (day <= 31))
			good = true;
		else if((month == 6) && (day <= 30))
			good = true;
		else if((month == 7) && (day <= 31))
			good = true;
		else if((month == 8) && (day <= 31))
			good = true;
		else if((month == 9) && (day <= 30))
			good = true;
		else if((month == 10) && (day <= 31))
			good = true;
		else if((month == 11) && (day <= 30))
			good = true;
		else if((month == 12) && (day <= 31))
			good = true;
	   
	   return good;


Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

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

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

Re: date validation

Posted 30 August 2009 - 07:00 PM

Never though of replacing

...
	   else if((month == 4) && (day <= 30))
			good = true;
		else if((month == 5) && (day <= 31))
			good = true;
...


by
int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

   if(day < daysInMonth[month])
	 good = true;



you like to type code son :)
Was This Post Helpful? 1

#15 hidden_within  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 07-April 09

Re: date validation

Posted 31 August 2009 - 08:45 PM

View Postpbl, on 30 Aug, 2009 - 06:00 PM, said:

Never though of replacing

...
	   else if((month == 4) && (day <= 30))
			good = true;
		else if((month == 5) && (day <= 31))
			good = true;
...


by
int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

   if(day < daysInMonth[month])
	 good = true;



you like to type code son :)

Thanks for the advice.
Here's the final method with the added suggestions
 public boolean validate()
	{
	 
	 	int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	 
	 	if(day <= 0)
		  good = false;
	  else if(day <= daysInMonth[month])
	 		good = true;
	  else if(month == 2)//check for leap years
		  {		
		 	boolean isleap = true;
			if(year % 4 != 0)
			   isleap = false;
			else if((year % 100 == 0) && (year % 400 == 0))
			   isleap = true;
			if((isleap == true) && (day <= 29))
					good = true;
			else if((isleap == false) && (day <= 28))
					good = true;
		}//end if
		  return good;
	}//end method validate


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1