Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using || in Cases in a Switch?

So for a college lab for Java Fundamentals I'm having trouble. I have to set up a switch and inside that switch a case. There are 3 options for user input, and each option can be answered with letter. The problem is that this letter is allowed to be capital OR lower case, and the problem is I cant seem to figure out how to set it up so a case will allow either of those.

In the code below..crustType is defined as a char.

Keep in mind this is Java Fundamentals and we're just learning about switches, unfortunately our PPT for this doesn't explain what to do in this situation.

switch (crustType)
  {
     case (crustType == 'H' || crustType == 'h'):
        crust = "Hand-tossed";
        System.out.println("You have selected 'Hand-Tossed' crust for your pizza.");
        break;

     case (crustType == 'T' || crustType == 't'):
        crust = "Thin-crust";
        System.out.println("You have selected 'Thin-Crust' crust for your pizza.");
        break;

     case (crustType == 'D' || crustType == 'd'):
        crust = "Deep-dish";
        System.out.println("You have selected 'Deep-Dish' crust for your pizza.");
        break;

     default:
        crust = "Hand-tossed";
        System.out.println("You have not selected a possible choice so a Hand-tossed crust was selected.");
  }

However I keep getting an error with ||...

97: error: incompatible types
      case (crustType == 'H' || crustType == 'h'):
                             ^   required: char   found:    boolean 
102: error: incompatible types
like image 426
WillBro Avatar asked Feb 03 '14 05:02

WillBro


People also ask

Does switch case use == or ===?

In answer to your question, it's === .

Can you use || IN case statement Ruby?

If you are eager to know how to use an OR condition in a Ruby switch case: So, in a case statement, a , is the equivalent of || in an if statement.


1 Answers

Use:

case 'H':
case 'h':
    ...
    break;
case 'T':
case 't':
    ...
    break;

instead. Since the type of crustType is char, then what goes in cases must be of char type. When you put something like

crustType == 'H'

you will get an error because that expression returns a boolean.

like image 148
Christian Tapia Avatar answered Oct 13 '22 23:10

Christian Tapia