Date Validation Java when user inputs 3 integers (MM, dd, yyyy)

18,895

Solution 1

You don't tell us what data causes the "return nothing" behavior you describe, but it looks to me like many of the times that don't pass your tests do a return statement out of main() without printing anything. For example: month > 12, you return. year % 4 == 0, you return.

ANYWHERE you put "return" out of main, put a println to indicate what you found.

Or learn to use a debugger.

Solution 2

Don't use return inside the if statements that's your problem. Make a boolean in the beginning called like isTrueDate, make it true, and if one of the conditions occur to make it not a true date, set it to false.

So something like ...

year = sc.nextInt();

boolean isTrueDate = true;

if(month > 12)
{
   isTrueDate = false;
}

and do this for everytime the date is not a real date.

At the end put

if (isTrueDate) {
   System.out.println("True.");
}
else {
   System.out.println("False.");
}
Share:
18,895
Bob
Author by

Bob

Updated on June 28, 2022

Comments

  • Bob
    Bob almost 2 years

    Write a program that receives 3 integer inputs, which represent a month, a date, and a year (for example, input of 9 23 2013 represents September 23, 2013). The program outputs whether or not the input represents a valid date. Keep in mind that April, June, September, and November have 30 days, February has either 28 or 29 days (depending on whether it is a leap year), and the other months have 31 days. Leap years are divisible by 4, with the exception of years at the end of each century (such as 2000), which are not leap years. You may assume that the user inputs only positive integers.

    This is the code I have so far, I've been using if/else statements which I know are not efficient but since I am a beginner at java it did not matter much to me. The program works fine for certain checks but when it doesn't it skips most of my code and doesn't return anything. tried messing with the brackets but no luck. Any help?

                package calendar;
                import java.util.Scanner;
    
                public class Calendar 
                {
                    public static void main(String[] args)
                    {
                        Scanner sc = new Scanner(System.in);
                        int month;
                        int day;
                        int year;
                        System.out.println("Enter your month:");
                        month = sc.nextInt();
                        System.out.println("Enter your day:");
                        day = sc.nextInt();
                        System.out.println("Enter your year:");
                        year = sc.nextInt();
    
                        if(month > 12)
                        {
                            return;
                        }
                        else if (year % 4 == 0)
                        {
                            return;
                        }
                        else if (month == 1 || month == 3 || month == 5 || month ==  7 || month ==  8 || month ==  10 || month == 12)
                            {
                            if (day == 31)
                            {
                                return;
                            }
                            else if (day != 31)
                            {
                                System.out.println("False.");
                            }
                        else if (month == 4 || month == 6 || month == 9 || month == 11)
                        {
                            if (day == 30)
                            {
                                return;
                            }
                            else if (day != 30)
                            {
                                System.out.println("False.");
                            }
    
                        }
                        else if (month == 2) // February check
                        {
                            if (year % 4 == 0) // Leap year check for February
                            {
                                if (day == 29)
                                {
                                    return;
                                }
                                else if (day != 29)
                                {
                                    System.out.println("False.");
                                }
                            else if (year % 4 != 0)
                            {
                                if (day == 28)
                                {
                                    return;
                                }
                                else if (day != 28)
                                {
                                    System.out.println("False.");
                                }
                            }
                            }
                        }
                        else // Everything checks out
                        {
                            System.out.println("True.");
                        }
    
                }
                }
                }
    
  • arcy
    arcy over 9 years
    We cannot help you with things unless you give us all the info. It should be obvious, even to a newbie, that the values that you've entered have everything to do with the execution path. So for any behavior you don't understand, you need to tell us what values you entered. Also, "until I passed the code to check for months 4,6,9,11" doesn't really tell me what line was executed; it would be better if you could say "It got to the 'if month == 4' line, then next executed the line '<whatever it is>', so that we can follow what you saw.
  • arcy
    arcy over 9 years
    But you said that, for certain values, it didn't print anything; the examples you give us all print something. On a quick check, it appears that all of these have printed the correct value.