-2

I am having an issue with a worksheet online and I was wondering if someone could show me the correct code for my problem and also explain as to why it should be written like that.

Here is the question on the worksheet I am on:

This time let's try a switch statement to convert the characters on a telephone dialpad to the corresponding digits. Recall that

'A', 'B', 'C' map to 2 'D', 'E', 'F' map to 3 'G', 'H', 'I' map to 4 'J', 'K', 'L' map to 5 'M', 'N', 'O' map to 6 'P', 'Q', 'R', 'S' map to 7 'T', 'U', 'V' map to 8 'W', 'X', 'Y', 'Z' map to 9

Write a switch statement that sets the variable digit to the appropriate digit, given the character letter. Set digit to 0 for any other character not listed in the mapping above.

Here is my attempt at solving the issue using a switch statement:

switch (letter)
{

case 'A' || 'B' || 'C' : digit = 2;

break;

case 'D' || 'E' || 'F' : digit = 3;

break;

case 'G' || 'H' || 'I' : digit = 4;

break;

case 'J' || 'K' || 'L' : digit  = 5;

break;

case 'M' || 'N' || 'O' : digit = 6;

break;

case 'P' || 'Q' || 'R' || 'S' : digit = 7;

break;

case 'T' || 'U' || 'V' : digit = 8;

break;

case 'W' || 'X' || 'Y' || 'Z' : digit = 9;

break;

default : digit = 0;

break;
}

How should I do this switch statement and what should be corrected??

I get the error message: I found more elements than I expected, here:

Yarry T
  • 151
  • 3
  • 15
  • You cannot use those ORs in a case statement like that. Google for the syntax of the switch statement: you need a "case" for each value. – Darius X. Jun 17 '13 at 15:20

1 Answers1

9

It should be:

case 'A': case 'B': case 'C':
    // do something

And as code styles vary, you may also see:

case 'A':
case 'B':
case 'C':
    // do something

This does the same thing.

Side node: 'A' || 'B' is not legal since || is a logical boolean operator which expects booleans on both sides; but here what you have are characters.

fge
  • 119,121
  • 33
  • 254
  • 329
  • Here is a short snippet of what I have changed it to: switch (letter) { case 'A' : case 'B' : case 'C' : digit = 2; break; Is this correct or not as I am still getting the same error?? – Yarry T Jun 17 '13 at 15:22
  • Seems good, whats the error? – Richard Tingle Jun 17 '13 at 15:46
  • The error was I found more elements than I expected, here: if you want to look at the worksheet it is here : http://labs.web-ide.org:8080/Web_IDE/#Lab-http://labs.web-ide.org/webide/labs/CLabs/selection.xml You just have to click on the switch section :) – Yarry T Jun 17 '13 at 15:49
  • yeah, thats not a real java exception, thats something the "web-IDE" is producing, using code that works correctly in a "real" IDE in it gives errors. Try using an IDE like netbeans or eclipse – Richard Tingle Jun 17 '13 at 15:57
  • I have eclipse its just this is the worksheet I use for my online course :) But if I were to use the code above that I put in for all cases then it would work?? If so awesome! – Yarry T Jun 17 '13 at 15:59
  • Yes, the comment box is going to shred it but the program in my following comment works for the first 6 letters – Richard Tingle Jun 17 '13 at 16:02
  • public static void main(String args[]) { char letter='d'; int digit; switch(letter){ case 'a': case 'b': case 'c': digit=1; break; case 'd': case 'e': case 'f': digit=2; break; default: digit=-1; break; } System.out.println(digit); } – Richard Tingle Jun 17 '13 at 16:03
  • Ok so I was right xD damn Web-IDE, thanks for all of your help Richard :) – Yarry T Jun 17 '13 at 16:10
  • Awesome, I have now had 30 seconds of experience with web-IDE and I already dislike it. I don't think its actually running your code, just pattern matching what it expects to see with what you enter – Richard Tingle Jun 17 '13 at 16:12
  • @RichardTingle http://ideone.com is pretty nice (fixed linkà – fge Jun 17 '13 at 16:14