+ 2

Convert IF-ELSE to SWITCH

Hi! I made a code that asks the user to enter the name of the month and prints whether the month has 30 days. I made it with if/else, but now I want to make the same one but with SWITCH. The switches are new to me so I would be very grateful if someone can help me. Much appreciate any help! Here is my original code https://code.sololearn.com/cl0KvWn5lJMN P.S. no need to write an entire code. Just one month that has 30 days, one that doesn't and how to handle the exception if the user entered "blah blah". Thanks a lot!

21st Jan 2018, 9:28 PM
DIY Mods
DIY Mods - avatar
11 Answers
+ 7
//import java.util.Scanner; //main Scanner scn = new Scanner(System.in); String input = scn.nextLine(); switch(input.toLowerCase()){ case "january": System.out.println(input +" Has 31 days"); break ; default : System.out.println("Error"); }
21st Jan 2018, 9:39 PM
D_Stark
D_Stark - avatar
+ 7
no problem i edited my post i missed a semicolon 😃
21st Jan 2018, 9:51 PM
D_Stark
D_Stark - avatar
+ 5
//import java.util.Scanner; //main Scanner scn = new Scanner(System.in); String input = scn.nextLine(); if( input.equalsIgnoreCase("January) || input.equalsIgnoreCase("March") ) {System.out.println(input + " 31 Days");}
21st Jan 2018, 10:17 PM
D_Stark
D_Stark - avatar
+ 5
i broke it down for you to read easier you use if, else for conditions like this not switch case
21st Jan 2018, 10:33 PM
D_Stark
D_Stark - avatar
+ 5
ChaoticDawg is great she/he helped me when i first began feel free to mark chaotic's answer best as its more suited for beginners 👍
22nd Jan 2018, 12:53 AM
D_Stark
D_Stark - avatar
+ 4
@DIY Mods You can use fall through and group the cases that will have the same output together instead of trying to use an OR || etc. You do this by omitting the break statement from the case(s) you want to fall through to the next case and so on. Here's your code converted to use fall through: switch (month) { case "April": case "June": case "September": case "November": System.out.println(month + " is correct! This month has 30 days"); break; case "January": case "February": case "March": case "May": case "July": case "August": case "October": case "December": System.out.println("Sorry, " + month + " has more or less then 30 days"); break; default: System.out.println("Invalid input. Try to type the name of the month from the capital letter"); break; }
21st Jan 2018, 11:50 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
Thank you, Dawg! You helping me the second day in a row. I don't even know how to share the best answer between you, guys) I like the way you grouped cases, and this is exactly what I needed, but Stark helped me first, so I'll leave it as is this time, hope you don't mind. Again thanks, a lot for your help!
22nd Jan 2018, 12:48 AM
DIY Mods
DIY Mods - avatar
+ 3
Here's a free video course that may help you further then: https://caveofprogramming.teachable.com/p/java-for-complete-beginners It may not contain all the newer features of Java 8 and 9, but should help you to grasp things better and give you a solid foundation.
22nd Jan 2018, 12:09 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
You are awesome! Thank you so much! I have a terrible professor who bubbling entire lecture as Eminem, and doesn't explain anything. Half of the class disappeared after the very first week. He almost made me drop the class, but I'm a stubborn dude, and I'll take it no matter what. Thanks again for your help!
22nd Jan 2018, 12:03 AM
DIY Mods
DIY Mods - avatar
+ 1
Got it! Thanks a lot!
21st Jan 2018, 9:47 PM
DIY Mods
DIY Mods - avatar
+ 1
One more question if you don't mind: Code is ready, but I thought that switches should help to minimize the amount of code. Instead, it 3 times larger. Is it possible to group months which is correct and which is incorrect to don't write system.out.println every time? Something like: case "April || June || September || November"; System.out.println("Correct!"); case "January || Ferbruary || May || etc" System.out.println("Sorry, incorrect"); Here is my code with switches: https://code.sololearn.com/cdfLFxym5dB1 Thank you in advance!
21st Jan 2018, 10:08 PM
DIY Mods
DIY Mods - avatar