8 Replies - 4199 Views - Last Post: 28 October 2012 - 07:23 PM Rate Topic: -----

#1 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 10-August 12

Date validation program / leap year...not correct results

Posted 28 October 2012 - 11:51 AM

I'm supposed to create a program that accepts a month, date, and year and it is supposed to check the validity of each of those as well as compute if the year is a leap year...

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Date validation program / leap year...not correct results

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Date validation program / leap year...not correct results

Posted 28 October 2012 - 11:59 AM

Use a debugger to step through the code or do loads of print statements. It's not that hard.
Was This Post Helpful? -1
  • +
  • -

#3 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 10-August 12

Re: Date validation program / leap year...not correct results

Posted 28 October 2012 - 12:02 PM

View Postdarek9576, on 28 October 2012 - 11:59 AM, said:

Use a debugger to step through the code or do loads of print statements. It's not that hard.


the debugger is not catching any problems with the program but the program is still yielding incorrect results. If the debugger was showing me anything it would have been included in my post. I appreciate your failed attempts to try and patronize me though.
Was This Post Helpful? 0
  • +
  • -

#4 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Date validation program / leap year...not correct results

Posted 28 October 2012 - 12:10 PM

The problem lies here:
		
if (month == 2 && leapYear == true)
{
   daysInMonth = 29;
} 
else
{
   daysInMonth = 28;
}

daysInMonth will now be set to 28 on every occation except when it's febuary and leap year.
What you want is to change the else to else if.
		
if (month == 2 && leapYear == true)
{
   daysInMonth = 29;
}
else if(month == 2)
{
   daysInMonth = 28;
}


Edit: or better yet, combine it with the previous if statements:
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;
}
else if (month == 2 && leapYear == true)
{
   daysInMonth = 29;
} 
else
{
   daysInMonth = 28;
}

This post has been edited by Kakerergodt: 28 October 2012 - 12:13 PM

Was This Post Helpful? 1
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Date validation program / leap year...not correct results

Posted 28 October 2012 - 12:22 PM

Your logic is obviousely wrong, so get a pen and paper and get cracking. Write stuff down that you expect to happen and next to it the stuff that you are getting. It's your homework not mine.
Was This Post Helpful? 0
  • +
  • -

#6 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 10-August 12

Re: Date validation program / leap year...not correct results

Posted 28 October 2012 - 12:43 PM

View PostKakerergodt, on 28 October 2012 - 12:10 PM, said:

The problem lies here:
		
if (month == 2 && leapYear == true)
{
   daysInMonth = 29;
} 
else
{
   daysInMonth = 28;
}

daysInMonth will now be set to 28 on every occation except when it's febuary and leap year.
What you want is to change the else to else if.
		
if (month == 2 && leapYear == true)
{
   daysInMonth = 29;
}
else if(month == 2)
{
   daysInMonth = 28;
}


Edit: or better yet, combine it with the previous if statements:
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;
}
else if (month == 2 && leapYear == true)
{
   daysInMonth = 29;
} 
else
{
   daysInMonth = 28;
}


Thanks! I should have been more careful with my if / else if / else statements....


View Postdarek9576, on 28 October 2012 - 12:22 PM, said:

Your logic is obviousely wrong, so get a pen and paper and get cracking. Write stuff down that you expect to happen and next to it the stuff that you are getting. It's your homework not mine.


For some reason I was thinking it was your responsibility, my sincerest apologies.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Date validation program / leap year...not correct results

Posted 28 October 2012 - 06:35 PM

KISS !!!!
     int nbInMonth(int month, int year) {
        int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(isLeapYear(year))
            days[1] = 29;
        return days[months];
      }



You really complicate your life for nothing
Was This Post Helpful? 0
  • +
  • -

#8 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 10-August 12

Re: Date validation program / leap year...not correct results

Posted 28 October 2012 - 07:19 PM

View Postpbl, on 28 October 2012 - 06:35 PM, said:

KISS !!!!
     int nbInMonth(int month, int year) {
        int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(isLeapYear(year))
            days[1] = 29;
        return days[months];
      }



You really complicate your life for nothing


That does look quite easier, although i'm not sure i understand every part of it...also, this assignment was given before we had learned about arrays so they were kind of out of the question.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Date validation program / leap year...not correct results

Posted 28 October 2012 - 07:23 PM

View PostgraFight, on 28 October 2012 - 10:19 PM, said:

we had learned about arrays so they were kind of out of the question.

I am afraid NOT !!!
You are having an array of size 12 having the number of days in each month of the year
This array element [1] contains 29 if the year is a leap year an 28 in other cases
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1