0

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)

Community
  • 1
  • 1
Lezcer
  • 17
  • 1
  • 5

3 Answers3

2

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.

One of the core concepts of programming is scope. You must learn to think in terms of boxes nested within boxes, like the Russian nesting dolls. Within a switch statement, the break command's scope is just that one statement rather than your entire method. The switch statement is one layer of scope nested within the outer method. The break within the switch statement can only "see" its immediate containing box: the switch statement.

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • to be honest im new to java and the worksheet implies that i DO NOT utilize commands i haven't been taught yet. Its my second week of programming – Lezcer Mar 23 '20 at 02:31
0

I suggest to see the comment, and then execute the code yourself. I add some comments.

public class TestFormatDate{

     public static void main(String []args){
        System.out.println("Please enter the date you would like to split");
        System.out.println("Please make sure it is in the format DDMMYYYY");

        //your input:70702020
        int date = 70702020;

        //70
        int day = date / 1000000;
        //70
        int month = (date / 10000) - (day * 100);
        //2020
        int year = date - (date / 10000) * (10000);
        String suffix = null;
        String monthName = null;
        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;
            // day is 70, so the default is executed.
            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;
            // month is 70, so the default is executed.
            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";
            // month is 70, so the default is executed.
            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 + ".");
        }
        // day is 70, so the else is executed.
        else 
        {
            System.out.println("It is the " + day + suffix + " day of " + monthName + " in " + year + ".");
        }
     }
}
Xiao Yu
  • 244
  • 1
  • 8
0

You are using a switch instead of an if(blah blah) statement. Meaning, that the statement is true and producing now errors. Your code says nothing is wrong with you input. Your suffix will still print, but print empty quotes as it is an empty string I believe. I suggest checking for entry before using the switch. Or using "System.exit(1)" where your system will exit if incorrect input.

Edit: Also your day by calculation would equal "70" thus making other errors.

  • that was the whole point of using an incorrect date, because i wanted to see what it would output if i entered that, and somehow it shows it, what you are basically saying is switch to an if statement? – Lezcer Mar 23 '20 at 02:29
  • You have no fail safe in the code. If you wanted termination, thats the wrong way to use a switch. You would need to terminate statement in each switch. As of right now, Java thinks its ok. – Kenosha Trader Mar 23 '20 at 03:04