Forum Stats

  • 3,769,004 Users
  • 2,252,898 Discussions
  • 7,874,837 Comments

Discussions

how do I solve this switch case bug in Java?

User_3OWV8
User_3OWV8 Member Posts: 1 Green Ribbon

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?

Tagged:

Answers

  • Vasily Strelnikov-Oracle
    Vasily Strelnikov-Oracle Member Posts: 15 Employee

    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.