Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.4K Development
- 17 Developer Projects
- 139 Programming Languages
- 293.1K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 159 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 473 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
how do I solve this switch case bug in Java?

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?
Answers
-
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.