0

Hi this is my code that I have used for my application. Every thing works fine however in some instances when inputting data that would use case 2 the if-else statement and the nested if-else statement displays one long answer of the different System.out.print. What would I have to do to display one answer? I want the program to display if the date entered to show if its a valid date, if the date entered is a leap year as well as display an error if the day or month is entered wrong

package javaapplication18;
import java.util.Scanner;

/**
 *
 * @author Thurston Pietersen
 */
public class JavaApplication18 
{
        public static void main(String[] args) 
    {
        int sI,fI ,sL, month, day, year;
        String mm, dd, yyyy, date;
        Scanner keyboard = new Scanner(System.in);
        // TODO code application logic here
        System.out.print("Input date using the following format mm/dd/yyyy: ");
        date = keyboard.next();

        sL = date.length();

        fI = date.indexOf("/");
        sI = date.lastIndexOf("/");

        mm = date.substring(0,fI);
        month = Integer.parseInt(mm);
        dd = date.substring((fI+1),sI);
        day = Integer.parseInt(dd);
        yyyy = date.substring((sI+1),sL);
        year = Integer.parseInt(yyyy);


        switch (month)
        {  
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                if (day > 31)
                    System.out.print(date + " is an invalid date.");
                else 
                    System.out.print(date +  " is a valid date.");

            case 4:
            case 6:
            case 9:
            case 11:
                if (day > 30)
                    System.out.print(date + " is an invalid date.");
                else 
                    System.out.print(date +  " is a valid date.");
            case 2:
                if (year % 4 == 0)
                    if (day > 29)
                       System.out.print(date + " is an invalid day.");
                    else
                       System.out.print(date + " is a valid date and leap year.");
                else
                    if (day > 28)
                        System.out.print(date + " is an invalid day.");
                    else
                       System.out.print(date + " is a valid date.");
            default:
                System.out.println("The date: " + date + " has an invalid month");




        }       



    }

}
skiwi
  • 66,971
  • 31
  • 131
  • 216
420
  • 29
  • 1
  • 8
  • possible duplicate of [Why do we need break after case statements?](http://stackoverflow.com/questions/2710300/why-do-we-need-break-after-case-statements) – donfuxx Mar 29 '14 at 19:13
  • It's beside the point here, but that's not the correct way to check for a leap year. The correct way to handle dates in general is to use a built-in library because it's always more complicated than you think. 1900 was not a leap year, but 2000 was. Will 2100 be? What's the range of dates the code needs to handle? – Jeff Learman May 03 '18 at 17:24

1 Answers1

3

You're missing break in all your cases. Due to that, the switch falls through, and all the cases are executed. Insert break at appropriate places:

switch (month) {  
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if (day > 31)
                System.out.print(date + " is an invalid date.");
            else 
                System.out.print(date +  " is a valid date.");
            break;  // Here

        case 4:
        case 6:
        case 9:
        case 11:
            if (day > 30)
                System.out.print(date + " is an invalid date.");
            else 
                System.out.print(date +  " is a valid date.");
            break;  // Here

        case 2:
            if (year % 4 == 0)
                if (day > 29)
                   System.out.print(date + " is an invalid day.");
                else
                   System.out.print(date + " is a valid date and leap year.");
            else
                if (day > 28)
                    System.out.print(date + " is an invalid day.");
                else
                   System.out.print(date + " is a valid date.");
            break;  // Here

        default:
            System.out.println("The date: " + date + " has an invalid month");

    }       

And please make a habbit of always enclosing your ifs and elses with curly braces.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525