Campbell Ritchie wrote:Agree; if you are using enum elements as constants for the different cases, and you use every enum element as a label to acase, you will never reach default. But I can envisage two situations where that approach will fail:-
Jose Fa wrote:. . . for example sitch for a enum, and the precondition is that you always get this value of enum. . . .
1: Your user passes null. 2: Somebody enhances the enum by adding another element.
Stephan van Hulst wrote:Most of my switch statements look roughly like this:
Campbell Ritchie wrote:Welcome to the Ranch
Let's ask you a question. If your options run 1...7, what are you going to do it you get 0 or 8?Now, add a default to that. What are you going to do? Print a message, reassign a boolean, throw an exception? Maybe an exception is overkill, but without some other action, your user won't know to enter a new option.[code=java]do
int input = getInput(1, 7);