Date Validation Project

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 12126 Views - Last Post: 02 June 2010 - 11:31 PM Rate Topic: -----

#1 J4ck.exe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 10

Date Validation Project

Posted 02 June 2010 - 04:52 PM

I am having trouble with the following project on hand. I use Textpad to do my work in Java. This is what I'm supposed to complete.

Date Validation

In this exercise you will write a program that checks to see if a date entered by the user is a valid date in the second millenium. A skeleton of the program is in Dates.java. Open this program and save it to your directory. As indicated by the comments in the program, fill in the following:

1. An assignment statement that sets monthValid to true if the month entered is between 1 and 12, inclusive.

2. An assignment statement that sets yearValid to true if the year is between 1000 and 1999, inclusive.

3. An assignment statement that sets leapYear to true if the year is a leap year. Here is the leap year rule (there's more to it than you may have thought!):

If the year is divisible by 4, it's a leap year UNLESS it's divisible by 100, in which case it's not a leap year UNLESS it's divisible by 400, in which case it is a leap year. If the year is not divisible by 4, it's not a leap year.

Put another way, 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. So 1600 and 1512 are leap years, but 1700 and 1514 are not.

4. An if statement that determines the number of days in the month entered and stores that value in variable daysInMonth. If the month entered is not valid, daysInMonth should get 0. Note that to figure out the number of days in February you'll need to check if it's a leap year.

5. An assignment statement that sets dayValid to true if the day entered is legal for the given month and year.

6. If the month, day, and year entered are all valid, print "Date is valid" and indicate whether or not it is a leap year. If any of the items entered is not valid, just print "Date is not valid" without any comment on leap year.

Heres the skeleton code:
// ************************************************** **************
// 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

//Check to see if month is valid

//Check to see if year is valid

//Determine whether it's a leap year

//Determine number of days in month

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

//Determine whether date is valid and print appropriate message

}

}


Here is my code that I have so far completed. It contains some errors that I have no idea how to solve.
// ************************************************** **************
// 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.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
monthValid = (month >= 1 || month <= 12);
System.out.println ("Month is:"+ monthValid);
//Check to see if year is valid
yearValid = (year >= 1000 || year <= 1999);
System.out.println("Year is valid:" +yearValid);

//Determine whether it's a leap year
leapYear =((year % 4) == 0 && ((year % 100) != 0) || (year % 400) == 0);
System.out.println ("It's a Leap Year: " +leapYear);

//Determine number of days in month
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
daysInMonth = 31;
if (month == 4 || month ==6 || month == 9 || month == 11);
daysInMonth = 30;
if (month == 2);
daysInMonth = 28;
if (leapYear);
daysInMonth = 29;
//User number of days in month to check to see if day is valid
if ( month < 1 || month > 12 );
= dayValid;
else if ( day < 1 || day > month_length[month] )
dayValid = FALSE;

return (dayValid);
}
//Determine whether date is valid and print appropriate message 

This post has been edited by J4ck.exe: 02 June 2010 - 05:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Date Validation Project

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Date Validation Project

Posted 02 June 2010 - 05:42 PM

It would be helpful if you could post the error messages you get..
anyways, from a glance at your code I noticed that statement:
return (dayValid); 

inside the main method.
The main method is declared as void, so why are you returning a value?
And another question, where you return that value to?

also, your if statements are followed by ";"
In your case, I don't see a reason for it. You should remove them:
if (month == 2);//<<these 


I noticed that statement also:
= dayValid; 

a copy\paste mistake perhaps..

But as I said, better post the exact error messages you recieve.
Was This Post Helpful? 1
  • +
  • -

#3 J4ck.exe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 10

Re: Date Validation Project

Posted 02 June 2010 - 05:47 PM

error codes:

C:\Users\Shane\Desktop\Dates.java:53: illegal start of expression
	= dayValid;
	^
C:\Users\Shane\Desktop\Dates.java:54: 'else' without 'if'
	else if ( day < 1 || day > month_length[month] )
	^
C:\Users\Shane\Desktop\Dates.java:58: reached end of file while parsing
}
 ^
3 errors

Tool completed with exit code 1


Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Date Validation Project

Posted 02 June 2010 - 05:53 PM

First, go over my remarks in my previous post, and fix those (it should take care of the first 2 errors).
The third error indicates that you are missing a closing braces in the end of your class. in your code, you have 2 opening braces (the first start the class scope, the second start the main method scope), but you have only one closing braces (to close the main method). add "}" at the end of your code.
Was This Post Helpful? 1
  • +
  • -

#5 J4ck.exe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 10

Re: Date Validation Project

Posted 02 June 2010 - 05:58 PM

Okay well i updated it to fix the mistakes and now have two new errors

// ************************************************** **************
// 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.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
	monthValid = (month >= 1 || month <= 12);
	System.out.println ("Month is:"+ monthValid);
	//Check to see if year is valid
	yearValid = (year >= 1000 || year <= 1999);
	System.out.println("Year is valid:" +yearValid);

	//Determine whether it's a leap year
	leapYear =((year % 4) == 0 && ((year % 100) != 0) || (year % 400) == 0);
	System.out.println ("It's a Leap Year: " +leapYear);

	//Determine number of days in month
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
	daysInMonth = 31;
	if (month == 4 || month ==6 || month == 9 || month == 11);
	daysInMonth = 30;
	if (month == 2);
	daysInMonth = 28;
	if (leapYear);
	daysInMonth = 29;
	//User number of days in month to check to see if day is valid
	if ( month < 1 || month > 12 );
	else if ( day < 1 || day > month_length[month] )
	dayValid = FALSE;
}
}
//Determine whether date is valid and print appropriate message



C:\Users\Shane\Desktop\Dates.java:53: cannot find symbol
symbol  : variable month_length
location: class Dates
	else if ( day < 1 || day > month_length[month] )
	                           ^
C:\Users\Shane\Desktop\Dates.java:54: cannot find symbol
symbol  : variable FALSE
location: class Dates
	dayValid = FALSE;
	           ^
2 errors

Tool completed with exit code 1


Was This Post Helpful? 0
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Date Validation Project

Posted 02 June 2010 - 06:16 PM

In your Date class, I can see these errors,


    if ( month < 1 || month > 12 );
    else if ( day < 1 || day > month_length[month] )
    dayValid = FALSE;



Firstly, when initialising to a Boolean variable, its lower case so it will be false not FALSE.

I can also see a secicolon on your if statement, you dont need that.

Also, why have you got the bit in your code?
 month_length[month] 
Shouldnt it just be 31?

This code look something similar to this

Theres to if statements to check both conditions instead of if and else-if which you had.
   if ( month < 1 || month > 12 ){     	
	//do something
        dayValid = false;
	   }
   if ( day < 1 || day > 31 ){
	//do something
      dayValid = false;
	     }


This post has been edited by m-e-g-a-z: 02 June 2010 - 06:18 PM

Was This Post Helpful? 1
  • +
  • -

#7 J4ck.exe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 10

Re: Date Validation Project

Posted 02 June 2010 - 06:45 PM

okay so i have the program running now with no errors with this code

// ************************************************** **************
// 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.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
	monthValid = (month >= 1 || month <= 12);
	System.out.println ("Month is:"+ monthValid);
	//Check to see if year is valid
	yearValid = (year >= 1000 || year <= 1999);
	System.out.println("Year is valid:" +yearValid);

	//Determine whether it's a leap year
	leapYear =((year % 4) == 0 && ((year % 100) != 0) || (year % 400) == 0);
	System.out.println ("It's a Leap Year: " +leapYear);

	//Determine number of days in month
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
	daysInMonth = 31;
	if (month == 4 || month ==6 || month == 9 || month == 11);
	daysInMonth = 30;
	if (month == 2);
	daysInMonth = 28;
	if (leapYear);
	daysInMonth = 29;
	//User number of days in month to check to see if day is valid
 	if ( month < 1 || month > 12 ){
        //do something
        dayValid = false;
           }
   	if ( day < 1 || day > 31 ){
        //do something
     	 dayValid = false;
           }

       }
}
//Determine whether date is valid and print appropriate message



but if i test it with test data it doesnt come out right

Enter the month:
354329085
Enter the day:
32429
Enter the year:
32528
Month is:true
Year is valid:true
It's a Leap Year: true
Press any key to continue . . .


Was This Post Helpful? 0
  • +
  • -

#8 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Date Validation Project

Posted 02 June 2010 - 06:58 PM

Well for a start this line,

 monthValid = (month >= 1 || month <= 12);


Look at it, || is an OR Operator so your checking either 1 or another which is Wrong. Month could be 999999 which would satisfy month>1.

You need the AND operator, like this

 monthValid = (month >= 1 && month <= 12);


which will check e.g month 15 which will give false as month has to be more or equal to 1 or less or equal to 12.

Try and do this for Year and Date.
Was This Post Helpful? 1
  • +
  • -

#9 J4ck.exe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 10

Re: Date Validation Project

Posted 02 June 2010 - 07:09 PM

Ok thank you so much that worked. im almost done. i just need to do

6. If the month, day, and year entered are all valid, print "Date is valid" and indicate whether or not it is a leap year. If any of the items entered is not valid, just print "Date is not valid" without any comment on leap year.

im not quite sure how to do this step in code.

updated code:
// ************************************************** **************
// 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.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
 	monthValid = (month >= 1 && month <= 12);
	System.out.println ("Month is valid: " + monthValid);
	//Check to see if year is valid
	yearValid = (year >= 1000 && year <= 1999);
	System.out.println("Year is valid: " +yearValid);

	//Determine whether it's a leap year
	leapYear =((year % 4) == 0 && ((year % 100) != 0) || (year % 400) == 0);
	System.out.println ("It's a Leap Year: " +leapYear);

	//Determine number of days in month
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
	daysInMonth = 31;
	if (month == 4 || month ==6 || month == 9 || month == 11);
	daysInMonth = 30;
	if (month == 2);
	daysInMonth = 28;
	if (leapYear);
	daysInMonth = 29;
	//User number of days in month to check to see if day is valid
 	if ( month < 1 || month > 12 ){
        //do something
        dayValid = false;
           }
   	if ( day < 1 || day > 31 ){
        //do something
     	 dayValid = false;
           }

       }
}
//Determine whether date is valid and print appropriate message


Was This Post Helpful? 0
  • +
  • -

#10 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Date Validation Project

Posted 02 June 2010 - 07:17 PM

For this bit here,

        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
        daysInMonth = 31;
        if (month == 4 || month ==6 || month == 9 || month == 11);
        daysInMonth = 30;
        if (month == 2);
        daysInMonth = 28;
        if (leapYear);
        daysInMonth = 29;



You will need to remove the semicolons from the if statements as it is not logically right.

Something like this

if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
        	daysInMonth = 31;	
        }
        
        if (month == 4 || month ==6 || month == 9 || month == 11){
        	 daysInMonth = 30;	 
        }
       
        if (month == 2){
        	 daysInMonth = 28;	 
        }
       
        if (leapYear){
        daysInMonth = 29;
        }


Was This Post Helpful? 2
  • +
  • -

#11 J4ck.exe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 10

Re: Date Validation Project

Posted 02 June 2010 - 07:38 PM

ohhh Okay. i did that now thanks.
Was This Post Helpful? 0
  • +
  • -

#12 J4ck.exe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 10

Re: Date Validation Project

Posted 02 June 2010 - 08:28 PM

i still need help about my other reply
Was This Post Helpful? 0
  • +
  • -

#13 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 Project

Posted 02 June 2010 - 08:35 PM

View PostJ4ck.exe, on 02 June 2010 - 09:28 PM, said:

i still need help about my other reply

which one ?
Was This Post Helpful? 0
  • +
  • -

#14 J4ck.exe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-June 10

Re: Date Validation Project

Posted 02 June 2010 - 08:37 PM

View Postpbl, on 03 June 2010 - 02:35 AM, said:

View PostJ4ck.exe, on 02 June 2010 - 09:28 PM, said:

i still need help about my other reply

which one ?


this:

Ok thank you so much that worked. im almost done. i just need to do

6. If the month, day, and year entered are all valid, print "Date is valid" and indicate whether or not it is a leap year. If any of the items entered is not valid, just print "Date is not valid" without any comment on leap year.

im not quite sure how to do this step in code.

updated code:
// ************************************************** **************
// 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.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
 	monthValid = (month >= 1 && month <= 12);
	System.out.println ("Month is valid: " + monthValid);
	//Check to see if year is valid
	yearValid = (year >= 1000 && year <= 1999);
	System.out.println("Year is valid: " +yearValid);

	//Determine whether it's a leap year
	leapYear =((year % 4) == 0 && ((year % 100) != 0) || (year % 400) == 0);
	System.out.println ("It's a Leap Year: " +leapYear);

	//Determine number of days in month
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
                daysInMonth = 31;
     	   }

     	   if (month == 4 || month ==6 || month == 9 || month == 11){
     	            daysInMonth = 30;
     	   }g

     	   if (month == 2){
     	            daysInMonth = 28;
     	   }

     	   if (leapYear){
     	   daysInMonth = 29;
        }

	//User number of days in month to check to see if day is valid
 	if ( month < 1 || month > 12 ){
        //do something
        dayValid = false;
           }
   	if ( day < 1 || day > 31 ){
        //do something
     	 dayValid = false;
           }

       }
}
//Determine whether date is valid and print appropriate message


Was This Post Helpful? 0
  • +
  • -

#15 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 Project

Posted 02 June 2010 - 08:40 PM

if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
        	daysInMonth = 31;	
        }
        
        if (month == 4 || month ==6 || month == 9 || month == 11){
        	 daysInMonth = 30;	 
        }
       
        if (month == 2){
        	 daysInMonth = 28;	 
        }
       
        if (leapYear){
        daysInMonth = 29;
        }



Easiest one
int getDayInMonth(int month, int year) {
    int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if(isLeapYear(year)) {
        days[2] = 29;
    }
    return days[month];
}


This post has been edited by pbl: 02 June 2010 - 08:42 PM
Reason for edit:: Removed useless code

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2