• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch not working with chars

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to make a method that converts a char into morse code (returns a string). I know that you can't use strings with switch, but likewise I know that you can use chars. Why does the following always return ".----" (the text of the last case, meaning no other case was a match):



Any help would be greately appreciated.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello you can use string with switch in java 1.7
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can use a String, but I can think of alternatives to switch-case.
  • 1: Use a Map<Character, MorseString>
  • 2: Use a MorseString[] and you can get the index from the char’s integer value.
  • You might be better using a MorseString class than ordinary Strings.

    Your switch-case might work better without fall‑through. Try a few break;s!
     
    Sheriff
    Posts: 22783
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Switch statements scan for the matching case, then execute all code from all cases below that until a break statement is encountered or the switch statement ends. In your case, no matter what the value, the last case statement will always be executed. You need to add break to each and every case statement:


    [Edit] Congrats Campbell, you beat me to it
     
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Because the "case" statement has the annoying feature that after it executes, it "falls through" to the next case statement. (So it isn't just a giant if-else-else structure.) Put a "break" statement at the end of each of those cases, otherwise execution will just continue executing each of the cases down to the last one.

    It's amazing how many switch-case structures I have written where I forget to put "break" after every single "case". That's probably why I try to avoid using it.

    [Edit: Ouch! Not even second! ]
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:Because the "case" statement has the annoying feature that after it executes, it "falls through" to the next case statement. (So it isn't just a giant if-else-else structure.) Put a "break" statement at the end of each of those cases, otherwise execution will just continue executing each of the cases down to the last one.

    It's amazing how many switch-case structures I have written where I forget to put "break" after every single "case". That's probably why I try to avoid using it.


    Which makes it even more amazing to me that Java's doesn't allow Duff's device, IMHO one of the main reasons for enjoying the absence of a break.

    That said, I use switch often, simply because it makes sense to me. Who wants to look at a darn great 'if...else' list?
    Just remember those breaks.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Spoor wrote: . . . [Edit] Congrats Campbell, you beat me to it

    Thank you I might write it in a diary; it happens so rarely
     
    Kirill Chernyshov
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ah, thanks! I'll remember that for the future.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic