Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Testing if dates are real or not.

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Testing if dates are real or not.

    I'm having some trouble with this code that asks for input and then displays if it is a real date or not. We were given the first code and we had to write the second code based on a provided UML diagram. I finished writing the second one and got it to compile but it displays the wrong results such as Invalid Date: No such month every single time. I've reviewed the code several times and the logic makes sense to me, what am I missing?

    /*
    	Test program for Date class
    */
     
    import java.util.Scanner;
     
    public class Assignment5
    {
    	/* Test driver for Date class
     
    		Prompt for the number of Date objects to test, then create Date objects and determine
    		if they are valid dates.
     
    	*/
     
    	public static void main (String [] args)
    	{
    		Scanner keyboard = new Scanner(System.in);
     
    		int monthVal;
    		int dayVal;
    		int yearVal;
     
    		int maxLoop;
     
    		System.out.println ("Enter number of dates to test");
    		maxLoop = keyboard.nextInt();
     
    		for (int time = 0; time < maxLoop; time++)
    		{
    			System.out.println ("Enter the month, day and year separated by a space");
    			monthVal = keyboard.nextInt();
    			dayVal = keyboard.nextInt();
    			yearVal = keyboard.nextInt();
     
    			Date aDate = new Date (monthVal, dayVal, yearVal);
     
    			if (aDate.isValid())
    				System.out.println ("Valid Date: " + aDate);
    			else
    				System.out.println ("Invalid Date: " + aDate);
    			System.out.println();
    		}
    	}
    }


    and then here is the second code:

    public class Date
    {
    	private int month;
    	private int day;
    	private int year;
     
    	public Date (int month, int day, int year)
    	{
    		int newMonth = month;
    		int newDay = day;
    		int newYear = year;
     
    	}
     
    	public boolean isValid()
    	{
    		if (year >= 1582)
     
    			if(month >= 1 && month <=12)
     
    				if (month == 4 || month == 6 || month == 9 || month== 11)
     
    					if (day >=1 && day <= 30)
    						return true;
    					else
    						return false;
    				else
    					if (month == 2)
    						if (isLeapYear())
    							if (day >=1 && day <= 29)
    								return true;
    							else
    								return false;
    						else
    							if (day >= 1 && day <=28)
    								return true;
    							else
    								return false;
     
     
     
    					else
    						if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    							if (day >=1 && day <= 31)
    								return true;
    							else
    								return false;
    						else
    							return false;
     
    			else
    				return false;
    		else
    			return false;
     
    	}
     
    	public String toString()
    	{
    		switch (month)
    		{
    			case 1:
    				return "January" + "," + day + "," + year;
    			case 2:
    				return "February" + "," + day + "," + year;
    			case 3:
    				return "March" + "," + day + "," + year;
    			case 4:
    				return "April" + "," + day + "," + year;
    			case 5:
    				return "May" + "," + day + "," + year;
    			case 6:
    				return "June" + "," + day + "," + year;
    			case 7:
    				return "July" + "," + day + "," + year;
    			case 8:
    				return "August" + "," + day + "," + year;
    			case 9:
    				return "September" + "," + day + "," + year;
    			case 10:
    				return "October" + "," + day + "," + year;
    			case 11:
    				return "November" + "," + day + "," + year;
    			case 12:
    				return "December" + "," + day + "," + year;
    			default:
    				return "No such month";
    		}
    	}
     
    	private boolean isLeapYear()
    	{
    		if (year % 4 == 0)
     
    			if (year % 100 == 0 && year % 400 !=0)
    				return true;
     
    			else
    				return false;
     
    		else
    			return false;
    	}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Testing if dates are real or not.

    Take a close look at your Date constructor. By the way giving your class the same name as a class in the Java API can only lead to confusion.

  3. The Following User Says Thank You to Junky For This Useful Post:

    cardsfan33 (March 22nd, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Testing if dates are real or not.

    Thank you I see the problem now within my constructor, and that part is functioning. Everything works except the leap year part. It still see February 29, 2008, for example, as an invalid date.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Testing if dates are real or not.

    Obviously your algorithm in the isLeapYear method is wrong. Google for the correct algorithm.

  6. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Testing if dates are real or not.

    Thanks once again! Much appreciated!

Similar Threads

  1. Java/Excel integration, Reading in Dates
    By aussiemcgr in forum JDBC and Database Tutorials
    Replies: 0
    Last Post: July 16th, 2010, 08:38 AM
  2. UTC Dates
    By PedroCosta in forum Java Theory & Questions
    Replies: 4
    Last Post: April 1st, 2010, 11:39 AM
  3. Storing data from a socket to a file in real time
    By colossusdub in forum Java Networking
    Replies: 0
    Last Post: March 2nd, 2010, 09:10 AM
  4. Changing colors of large image in real time
    By chals in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 7th, 2009, 05:06 AM
  5. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:08 PM