1
\$\begingroup\$

I know this task could be done in more practical ways, but the professor only want us to solve it with the "if" statement. I tested and it meets the condition for each task, but I still feel this code looks a bit awkward, I think there is an easier way to get it done. Can someone give me any advice?

import javax.swing.JOptionPane;
public class ValidDate {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String Smonth =
                JOptionPane.showInputDialog ("Enter the Month");
        int month = Integer.parseInt(Smonth);

        if (month <= 0 || month > 12)
        {
            JOptionPane.showMessageDialog (null, " The month you entered is wrong" );
            System.exit (0);
        }

        String Sday =
                JOptionPane.showInputDialog ("Enter the Day");
        int day = Integer.parseInt(Sday);

         if (day <= 0 || day > 31)
            {
                JOptionPane.showMessageDialog (null, " The day you entered is wrong" );
                System.exit (0);
            }
         else if (month == 4 || month == 6 || month == 9 || month == 11 && day > 30)
            {
                JOptionPane.showMessageDialog (null, " The day you entered is wrong because " + month + " month has only 30 days");
                System.exit (0);
            }

        String Syear =
                JOptionPane.showInputDialog ("Enter the Year");
        int year = Integer.parseInt(Syear);
        boolean leap = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));

         if (leap == false && month == 2 && day >=29)
        {
            JOptionPane.showMessageDialog (null, " The Date you entered is wrong since " + year +" is not a leap year" );
            System.exit (0);
        }
        else 
            JOptionPane.showMessageDialog (null, " The Date you entered is " + month + "/" + day + "/" + year);

    }

}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Are you required to prompt for month, day, year in that particular order? \$\endgroup\$ Commented Mar 30, 2017 at 16:22

2 Answers 2

1
\$\begingroup\$

I'm not clear on your requirements. I don't remove any of your if statements, so I'll claim to meet them. It's possible your teacher would disagree.

Constants

         else if (month == 4 || month == 6 || month == 9 || month == 11 && day > 30)

Rather than manually writing out the days for each, consider creating a constant

    public static final int[] daysIn = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

Then you can just say

        else if (day > daysIn[month])

That would also allow you to just check the beginning of the month in the first if.

Methods

        boolean leap = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));

         if (leap == false && month == 2 && day >=29)

You do not need to check for leap years for every date. You only need to check February when it is the 29th. Consider

         if (month == 2 && day == 29 && !isLeap(year))

Now you only check for leap years if someone enters what would be a leap day.

It's a simple method that the compiler will probably inline.

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

But this way the code is readable and efficient.

Also consider

    public static int inputInteger(String prompt)
    {
        String input = JOptionPane.showInputDialog(prompt);
        return Integer.parseInt(input);
    }

Then you don't have to declare two variables in main for every actual value you want to collect. Just

    int month = inputInteger("Enter the month");

Advanced

Consider showing the month names rather than the numbers.

Rather than doing it with generic text boxes, consider using dropdown menus. Then the values will always be correct because you can only select valid values. For February, this means that you have to get the year before the day (I'd recommend first, before month as well).

For really advanced, show a twelve month calendar and let people pick the exact day of the exact month.

The latter two solutions probably won't meet your teacher's criteria. Not enough if statements.

\$\endgroup\$
1
\$\begingroup\$

First, a few comments about style.

  • You shouldn't start variable names with a capital letter, that's used for constants and class names. So instead of Smonth and Sday, use monthStr and dayStr.
  • If you put braces around the statement after an if, put them around the else as well. Your last if/else doesn't do that.
  • Instead of saying leap == false, say !leap.

I think the one thing that could make your code look cleaner and clearer would be to move repeated code off into a separate method. For example, you're repeatedly opening a JOptionPane, getting a value and then converting it to an integer. Why not put that in a separate method, so you could call:

int month = getValue("Month");

This would reduce the clutter in your main method and have the added advantage that if you wanted to change how you ask the user for values, you would only have to do it in one place. Do that with your error checking as well and your code improve quite a bit.

\$\endgroup\$
1
  • \$\begingroup\$ You should check if the requirements say it is sufficient to support only the Gregorian calendar. Historians are likely to enter dates from the Julian calendar. There are people alive today who were born in countries where the Julian calendar was in use when they were born, so may give their birth date in that calendar. \$\endgroup\$ Commented Mar 31, 2017 at 2:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.