user5947618
user5947618

Reputation:

Validating date from user input

I'm writing a program where I am supposed to have the user input a date from the year 0 - 4000. I'm supposed to see if the date is valid, and if it was a leap year or not. I'm having problems in my code. I'm getting an else without error on line 57. I also am not sure how to how to say if the date is valid or not. IE: this date is valid, is a leap year - or is not valid is not a leap year ...etc...

I'm still a beginner so I dont want the code written for me but I would like to know how to fix it! Thank you.

import java.util.*;

public class LegalDate   //file name
{
        public static void main (String [] args)

    {
        Scanner kb = new Scanner (System.in); //new scanner
        //name the variables
        int month, day, year;
        int daysinMonth;
        boolean month1, year1, day1;
        boolean validDate;
        boolean leapYear;



        //ask the user for input
        //I asked the MM/DD/YYYY in seperate lines to help me visually with the program
        System.out.println("Please enter the month, day, and year  in interger form: " );
        kb.nextInt();

        //now I'm checking to see if the month and years are valid
        if (month <1 || month >12)
            { month1 = true;}
        if (year <0 || year >4000)
            {year1= true;}

        //I'm using a switch here instead of an if-else statement, which can also be used


             switch (month) {
                case 1:
                case 3:
                case 5:             //months with 31 days
                case 7:
                case 8:
                case 10:
                case 12:
                     numDays = 31;
                     break;
                 case 4:
                 case 6:              //months with 30 days
                 case 9:
                 case 11:
                     numDays = 30;
                     break;

                 case 2:
                     if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))  //formula for leapyear
                         numDays = 29;
                            {
                                system.out.println("is a leap year");
                                }
                     else
                         numDays = 28;
                            {
                                system.out.println("is not a leap year");
                                }
                     break;
                default:
                     System.out.println("Invalid month.");
                break;

                     if (month1 == true)
                     if (day1 == true)
                     if (year1 == true)
                           System.out.println ("date is valid ");

                     else
                     if (month1 == false)
                           System.out.println ("date is invalid");

                     else
                     if (day1 == false)
                           System.out.println ("date is invalid");

                      else
                      if (year1 == false)
                           System.out.println ("date is invalid");



    }}

}

Upvotes: 1

Views: 2383

Answers (4)

Dimpl
Dimpl

Reputation: 942

You don't appear to be placing the code between your 'if' and 'else' statements in curly brackets, which means that the statement will only apply to the next line. For example:

if (a)
    b = true
    c = true
else
    d = true

is read as

if (a) {
    b = true
}
c = true
else {
    d = true
}

Hopefully, you can see how the compiler wouldn't understand this, as an 'else' statement must occur directly after its associated 'if' block.

I would suggest adding some methods to simplify your code. For example:

public static boolean isLeapYear(int year) {
    return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0));
}

Also, if you use boolean variables to store information, you can print it all neatly at the end. For example, you could instantiate a variable 'isValid' to true at the top of your code, set it to false if you calculate that the date is invalid, and use an if statement at the end to print your result.

I know you said you didn't want it written for you, but that's the easiest way to demonstrate the importance of methods. Hopefully you can see how this is more readable than your version?

import java.util.Scanner;

public class LegalDate {

    static final int maxYear = 4000;

    public static void main (String [] args) {
        int month, day, year;
        boolean leapYear, validDate = false;

        Scanner kb = new Scanner (System.in);
        System.out.println("Please enter the month, day, and year in interger form.");

        System.out.print("Month: ");
        month = kb.nextInt();
        System.out.print("Day: ");
        day = kb.nextInt();
        System.out.print("Year: ");
        year = kb.nextInt();

        leapYear = isLeapYear(year);
        validDate = isValidDate(month, day, year);

        System.out.printf("%nThe date is %svalid and is %sa leap year.%n", validDate ? "" : "not ", leapYear ? "" : "not ");
        kb.close();
    }

    public static int numDaysInMonth(int month, boolean isLeapYear) {
        switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
             return 31;
        case 4:
        case 6:
        case 9:
        case 11:
             return 30;
        case 2:
             if (isLeapYear) {
                 return 29;
             } else {
                 return 28;
             }
        default:
             return 0;
        }
    }

    public static boolean isLeapYear(int year) {
        return (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0));
    }

    public static boolean isValidDate(int month, int day, int year) {
        return (month >= 1 && month <= 12) && (day >= 1 && day <= numDaysInMonth(month, isLeapYear(year))) && (year >= 0 && year <= maxYear);
    }
}

If you have any questions I'll try my best to answer them!

Upvotes: 0

Rakesh K
Rakesh K

Reputation: 172

The syntax of if-else statement on line 50 of your program is not correct. It's a dangling else. Enclosing the body of if and else statement within braces should resolve this.

if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))  
{ 
    numDays = 29;                            
    system.out.println("is a leap year");
}
else
{
    numDays = 28;
    system.out.println("is not a leap year");
}

you can make use of IDE or give attention to compiler error message to resolve such errors.

Upvotes: 0

Noor Nawaz
Noor Nawaz

Reputation: 2225

Why don't you try Java 8 date time API

It validates date and do much more for you

Like

try {
        LocalDate date =LocalDate.of(2016, 12, 31);
        if(date.isLeapYear())
            System.out.println("Leap year");
        else 
            System.out.println("Not leap year"); 
}
catch(DateTimeException e) {
   System.out.println(e.getMessage());
}

Upvotes: 1

Nicholas Eason
Nicholas Eason

Reputation: 300

On line 57, you open a new code block but nothing is able to access it. I believe you meant to type:

else{
        numDays = 28;
        system.out.println("is not a leap year");
    }

As a small tip, you can change this:

if (month1 == true)
                 if (day1 == true)
                 if (year1 == true)
                       System.out.println ("date is valid ");

to this:

if (month1 && day1 && year1)
   System.out.println ("date is valid ");

Since the boolean comparison operators return true or false, you can tell that the condition just needs to be boolean. Since month1, day1, and year1 are all boolean values, you dont need to compare them to anything.

What the condition means, in the event you don't know, is if month1 and day1 and year1 are all true, then print date is valid

Upvotes: 1

Related Questions