I have to split a date inputted by a user in the format DDMMYYYY. however if you input 70702000 its displaying all 3 of my errors instead of one

Lezcer :

Below the code:

    {
        System.out.println("Please enter the date you would like to split");
        System.out.println("Please make sure it is in the format DDMMYYYY");
        date = sc.nextInt();

        day = date / 1000000;
        month = (date / 10000) - (day * 100);
        year = date - (date / 10000) * (10000);

        switch(day) 
        {
            case 1: case 21: case 31: 
                suffix = "st";
            break;

            case 2: case 22:
                suffix = "nd";
            break;

            case 3: case 23: 
                suffix = "rd";
            break;

            case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 24: case 25: case 26: case 27: case 28: case 29: case 30:
                suffix = "th";
            break;

            default:
                System.out.println("Error, Please enter a valid day (i.e. DD) that is between 01 - 31");
            break;
        }

        switch(month)
        {
            case 4: case 6: case 9: case 11: 
                if ((day < 1) || (day > 30))
                {
                    System.out.println("Error this day does not exist"); 
                }
            break;

            case 1: case 3: case 5: case 7: case 8: case 10: case 12: 
                if ((day < 1) || (day > 31))
                {
                    System.out.println("Error this day does not exist");
                }
            break;

            case 2:
                if ((day < 1) || (day > 28))
                {
                    if ((day != 29)) 
                    {
                        System.out.println("Error this day does not exist");
                    }
                    else if ((year % 4 != 0) || ((year % 400 != 0) && (year % 100 == 0)))
                    {
                        System.out.println("Error this day does not exist");
                    }
                        //If it isn't a leap year, febuary cannot have 29 days
                }
            break;
            default:
                System.out.println("Error this day does not exist");
        }

        switch(month)
        {
            case 1:
                monthName = "January";
            break;
            case 2:
                monthName = "Febuary";
            break;
            case 3: 
                monthName = "March";
            break;
            case 4:
                monthName = "April";
            break;
            case 5: 
                monthName = "May";
            break; 
            case 6: 
                monthName = "June";
            break;
            case 7:
                monthName = "July";
            break;
            case 8:
                monthName = "August";
            break;
            case 9: 
                monthName = "September";
            break; 
            case 10:
                monthName = "October";
            break;
            case 11: 
                monthName = "November";
            break;
            case 12:
                monthName = "December";
            default:
                System.out.println("Error, Please make sure the month (i.e. MM) is between 01 and 12");
            break;
        }

        if ((day == 29) && (month == 2))
        {
            System.out.println("It is the 29th day of Febuary in " + year + ".");
        }
        else 
        {
            System.out.println("It is the " + day + suffix + " day of " + monthName + " in " + year + ".");
        }
    }

So, basically when I run this program, and input 70702020 value, I'll get all the following outputs:

Error, Please enter a valid day (i.e. DD) that is between 01 - 31

Error this day does not exist

Error, Please make sure the month (i.e. MM) is between 01 and 12

And then it would output the final output (this was only intended if the user entered a valid date)

Basil Bourque :

break breaks out of switch statement only

The break commands in your switch statements bail out of the switch statement only, not your entire method.

You have a series of three switch statements.

  • When you break out of the first switch statement, the flow of control moves on to the second switch statement.
  • When you break out of the second switch statement, the flow of control moves on to the third switch statement.
  • When you break out of the third switch statement, the flow of control moves on to the remaining statements.

java.time

By the way, parsing as text via the modern java.time classes is much simpler.

try {
    LocalDate ld = 
            LocalDate
            .parse( 
                "70702020" , 
                DateTimeFormatter.ofPattern( "ddMMuuuu" ) 
            )
    ;
} catch ( DateTimeParseException e ) {
    … 
}

Invalid inputs throw a DateTimeParseException.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349462&siteId=1