Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how do I solve this switch case bug in Java?

User_3OWV8Jun 8 2021

Hello Guys! the question goes like this:
Create a while loop that will execute the following code 12 times using a counter that starts at 1 and is incremented by 1 each time through the loop.
Within the while loop, create a switch statement that will print out the number of days in each of the months of the year. Use fall-through capabilities so that you only have one print statement for all of the months that contain 31 days. Do not worry about leap years.
Print out the name of the month as well. You can copy and paste your month names array from the previous lab to help make this easier.
Here is my Answer:
int numOfMonth = 1;
String[] months ={"January", "February", "March", "April", "May", "June" , "July" , "August" , "September" , "October" , "November", "December"};
int[] daysOfMonths ={28, 30, 31};
// String output;

    while(numOfMonth++ <= 12){

        int index = numOfMonth -1;

        switch(numOfMonth){

            case 2:

                System.out.println(months[index] + " has " + daysOfMonths[0] + " Days.");
                break;

            case 4:
            case 6:
            case 9:
            case 11:
                System.out.println(months[index] + " has " + daysOfMonths[1] + " Days.");
                break;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                System.out.println(months[index] + " has " + daysOfMonths[2] + " Days.");
                break;
            default:
                break;
        }
    }

This Code outputs All other values Correctly except that It DOESN't OUTPUT JANUARY
Please, WHAt SHall I do?

Comments

There is a problem with an array index handling in the example you provided, remember that indexing is 0 based. Anyway, here's a bit more straightforward version of your code:

  int numOfMonth = 0;
  String[] months = {"January", "February", "March", "April", "May", "June", "July", 
                     "August", "September", "October", "November", "December"};
  int[] daysOfMonths ={28, 30, 31};
  while(numOfMonth < 12){
   switch(numOfMonth){
    case 1:
     System.out.println(months[numOfMonth] + " has " + daysOfMonths[0] + " Days.");
     break;
    case 3:
    case 5:
    case 8:
    case 10:
     System.out.println(months[numOfMonth] + " has " + daysOfMonths[1] + " Days.");
     break;
    case 0:
    case 2:
    case 4:
    case 6:
    case 7:
    case 9:
    case 11:
     System.out.println(months[numOfMonth] + " has " + daysOfMonths[2] + " Days.");
    }
    numOfMonth++;
  }

There are couple of other observations regarding this example I can offer - it is considered to be a better coding practice not to perform actions and boolean checks in the same expression, doing so makes code less readable. Hence probably the reason why you had an issue understanding were code goes wrong. Also, try to avoid creating excessive intermediate variables.

1 - 1

Post Details

Added on Jun 8 2021
1 comment
530 views