HatTrick
HatTrick

Reputation: 13

Java Valid Dates and JOptionPane

Noob here so bear with me..I was working on a Java program that takes the users input from the JoptionPane, and uses a set of if-else statements to verify the date while considering the leap years. The problem I seem to be having when it runs is that it validates dates that aren't possible. For example, 2/29/2014, it says it's a valid date, but in reality it isn't. Insight on this issue would be highly appreciated.

 import java.util.Arrays;
 import javax.swing.JOptionPane;

 public class vaildDate {

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


    int month, day, year;

    String userInput = JOptionPane.showInputDialog("Enter a valid date in mm/dd/yyyy format: ");
    String [] date = userInput.split("/");
    String mm = date[0];
    String dd = date [1];
    String yyyy = date  [2];
    month  = Integer.parseInt(mm);
    day = Integer.parseInt(dd);
    year = Integer.parseInt(yyyy);
    System.out.println(mm+dd+yyyy);



    boolean validLeapYear;
    boolean validDate;
    if(month>=1 && month <=12&& day>=1 && day<=31)
    {

        if(month == 4 || month == 9 || month == 6 || month == 11 &&  day <= 30)
        {
            validDate = true; 
        }

        if(( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 11 || month == 12) && (day <= 31))
        {
            validDate = true;
        }
        else{ 
            validDate =false; 

        }

        if((year % 400 == 0 ) || (year % 4 == 0) && (year % 100 != 0))
        {

            validLeapYear = true;

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


        if(validDate = true)
        {
            JOptionPane.showMessageDialog(null, month +"/"+day+"/"+year+" is a valid date");
        }

        else if(validDate = false) {
            JOptionPane.showMessageDialog(null,"Your date is invalid");
        }
    }
   }
  }

Upvotes: 1

Views: 2853

Answers (2)

StoopidDonut
StoopidDonut

Reputation: 8617

Instead of equating booleans your are assigning the values which return the results of your assignment.

For example:

if (validDate = true)

The above statement would return true. Instead, in all your boolean checks just validate the boolean value as is.

if (validDate)

To remove the compiler errors, you can initialize your 2 boolean vars to false.

You changed code would look something like:

 public static void main(String[] args) {
    // TODO Auto-generated method stub
    int month, day, year;

    String userInput = JOptionPane
            .showInputDialog("Enter a valid date in mm/dd/yyyy format: ");
    String[] date = userInput.split("/");
    String mm = date[0];
    String dd = date[1];
    String yyyy = date[2];
    month = Integer.parseInt(mm);
    day = Integer.parseInt(dd);
    year = Integer.parseInt(yyyy);
    System.out.println(mm + dd + yyyy);

    boolean validLeapYear = false;
    boolean validDate = false;

    if (month >= 1 && month <= 12 && day >= 1 && day <= 31) {

        if (month == 4 || month == 9 || month == 6 || month == 11
                && day <= 30) {

            validDate = true;
        }

        if ((month == 1 || month == 3 || month == 5 || month == 7
                || month == 8 || month == 11 || month == 12)
                && (day <= 31)) {
            validDate = true;

        } else {

            validDate = false;

        }

        if ((year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0)) {

            validLeapYear = true;

        }
        if ((month == 2 && day == 29) && (!validLeapYear)) {

            validDate = false;
        }
        if ((month == 2 && day <= 29) && (validLeapYear)) {

            validDate = true;
        }

        if (validDate) {

            JOptionPane.showMessageDialog(null, month + "/" + day + "/"
                    + year + " is a valid date");
        }

        else if (!validDate) {
            JOptionPane.showMessageDialog(null, "Your date is invalid");
        }
    }
}

Upvotes: 2

Sylvain Cloutier
Sylvain Cloutier

Reputation: 388

A quick search on Google shows there are easier ways to validate a date. Please see this one:

http://www.mkyong.com/java/how-to-check-if-date-is-valid-in-java/

It uses the date parse and catches the exception if the date is not valid (cannot be parsed).

If you want to stick with your algorithm, I suggest to use an IDE like Eclipse and debug set by step while keeping a look on the value of your variables. You clearly have an algorithm problem, which will become obvious while debugging.

Hope that helps.

Upvotes: 1

Related Questions