0

I have a homework assignment which asks to have the user input a date in Java in the (mm/dd/yyyy) format, then to determine if the entered date is valid. I have been able to successfully do this for every month, save February, because you must take leap years into account.

I have this code:

import java.util.Scanner;

/**
 * 
 * @author Andrew De Forest 
 * @version v1.0
 * 
 */
public class exc6
{
    public static void main (String[] args)
    {  
        //Initialize a string
        String getInput;
        //Initialize some integers
        int month, day, year;
        //Make a boolean
        boolean validDate;
        //Set the date to false
        validDate = false;
        //Ask for input
        System.out.println("Enter a date (mm/dd/yyyy)");
        //Initialize the scanner
        Scanner keyboard = new Scanner (System.in);
        //Get input & use a delimiter
        keyboard.useDelimiter("[/\n]");
        month = keyboard.nextInt();
        day = keyboard.nextInt();
        year = keyboard.nextInt();

        if((month >= 1 && month <= 12) && (day >= 1 && day <= 31))
        {
            //For months with 30 days
            if((month == 4 || month == 6 || month == 9 || month == 11) && (day <= 30))
            {
                validDate = true;
            }

            //For months with 31 days
            if((month == 1 || month == 2 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31))
            {
                validDate = true;
            }

            //For February
            if((month == 2) && (day < 30))
            {
                //Boolean for valid leap year
                boolean validLeapYear = false;

                //A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400
                if((year % 400 == 0) || ((year % 4 == 0) && (year %100 !=0)))
                {
                    validLeapYear = true;
                }

                if (validLeapYear == true && day <= 29)
                {
                    validDate = true;
                }

                else if (validLeapYear == false && day <= 28)
                {
                    validDate = true;
                }
            }
        }

        //If the date is valid
        if(validDate == true)
        {
            System.out.println(month + "/" + day + "/" + year + " is a valid date.");
        }

        else
        {
            System.out.println("Invalid date!");
        }

    }
}

The part I'm most concerned with is this:

    //For February
    if((month == 2) && (day < 30))
    {
        //Boolean for valid leap year
        boolean validLeapYear = false;

        //A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400
        if((year % 400 == 0) || ((year % 4 == 0) && (year %100 !=0)))
        {
            validLeapYear = true;
        }

        if (validLeapYear == true && day <= 29)
        {
            validDate = true;
        }

        else if (validLeapYear == false && day <= 28)
        {
            validDate = true;
        }
    }
}

As far as I can tell, it looks correct. However, when I input something like 2/29/2011, it returns as a valid date (which it should not, as 2011 was not a leap year). Why is this? What am I missing, or passing over, that causes bad dates to return valid?

4
  • 2
    Try to make comments meaningful, and not just a duplicate of the code. Also, when posting code, try to remove things not relevant, for example, the opening javadoc adds nothing of value and takes up noticeable vertical space. Commented Feb 13, 2012 at 0:56
  • @Dave Newton I will keep this in mind, thank you Commented Feb 13, 2012 at 1:06
  • Can't you use any java library? java.util.Calendar, org.joda.time.DateTime?? Commented Feb 13, 2012 at 2:23
  • Normally, yes...but this particular assignment required us to follow specific directions ;) Commented Feb 13, 2012 at 3:09

3 Answers 3

4
if((month == 1 || month == 2 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31))

This line already catches February.

Sign up to request clarification or add additional context in comments.

1 Comment

For java 8, you have java.time.Month with static values for each month. To get as int just do : Month.July.getValue()
2
DateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd");
dateFormat.setLenient(false);
String dateAsString = "2011-Feb-29";
Date date = dateFormat.parse(dateAsString);  // throws an exception; invalid date

3 Comments

While indeed the right way, I'm not sure if the tutor allows using DateFormat for this homework assignment.
@duffymo I'm sure this would be the correct way to do this in the future, but for now the assignment is to pick apart the user's input ;)
Oh, well. Learning comes in all forms.
0

First you're setting validDate to true because month is 2.

Next you're setting validLeapYear to false because it's not a leap year.

(validLeapYear == true && day <= 29) isn't true.

(validLeapYear == false && day <= 28) also isn't true.

Therefore, validDate is still true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.