Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7447017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:29:22+00:00 2026-05-29T12:29:22+00:00

I have a homework assignment which asks to have the user input a date

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:29:23+00:00Added an answer on May 29, 2026 at 12:29 pm
    if((month == 1 || month == 2 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31))
    

    This line already catches February.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A recent homework assignment I have received asks us to take expressions which could
We've gotten a homework assignment in Java, which relates to inheritance. I don't have
I have a homework assignment where I need to take input from a file
Have a homework assignment in which I'm supposed to create a vector of pointers
I have a homework assignment to write a multi-threaded sudoku solver, which finds all
I have a homework assignment that asks to create an order form. The order
This was a homework assignment problem which I know I have incorrectly answered. I
(this is indirectly a part of a much larger homework assignment) I have something
Wrote up a basic file handler for a Java Homework assignment, and when I
I have to implement an API for a homework assignment, and my instructor has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.