Search code examples
javavalidationdateif-statementjoptionpane

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");
        }
    }
   }
  }

Solution

  • 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");
            }
        }
    }