• 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

Using switch with a string

 
Greenhorn
Posts: 4
Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written the following :

This should take in a string like "ATCG" and reverse it, but change the letters as shown in the switch block. However it always writes out "CCCC". What am I doing wrong ?
It does not matter if I replace text.charAt(i) with mh and decleare char mh = '';
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not breaking out of your switch cases. For instance, when the character matches 'T', it will go to the according case, and assign "A" to c. However, it then continues to case 'C' and after that 'G'. When you're done with a case, break out of it by using the break keyword.
 
M. Raven
Greenhorn
Posts: 4
Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, OK. So it performs case 'G': c = "C"; even if the value to switch is 'T' and not 'G'? That's a bit odd isn't it?

What would the code here http://www.java2s.com/Code/Java/Data-Type/switchwithcharvalue.htm
then essentially ignore everything until case: 'a' ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, it will perform all three cases starting with T, each time overwriting the old value. That's how the language designers chose to make the switch statement work, which was probably a mistake. We're stuck with it now though.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer it this way. The alternative is something like the SELECT statement in Visual Basic (ugh), where it's impossible for the cases to share any code. If I have to add a few break statements to prevent code sharing, so be it. I think it's better to let the programmer decide whether or not he wants to have the cases share code, not the language.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then why not allow cases to repeat themselves in different combinations? It definitely seems a lot more intuitive to me than cases that fall through, and it's more flexible as well, because cases that "need to do more" don't necessarily have to appear before cases they fall into.
Here, when the day is Thursday, we goToGym() and then partyHardy().
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure that's immediately intuitive, though it could be workable. Would THURSDAY match both options? Or was that a typo? (Edit: I obviously didn't read your last sentence )

C# has a compromise - you can't fall through, but you can "go to" another case statement. So that example would be something like this:
Not quite as immediately obvious, but it does mean you can avoid duplication without falling through by accident.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C# has a combination of breaks and gotos? That looks absolutely horrible

It looks like a language within a language.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not really horrible, it's just used in that specific context. Every case must end in either a break or a goto case statement (which can only switch to another case, not go to anywhere else). I must admit, I don't think I've actually used the feature yet in real code, but it does seem to me to be a compromise between the two different positions mentioned in this thread.
 
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
Two comments:

(1) Matthew's code example is hard to understand; at least when I looked at it, it wasn't obvious to me what it did. And it's only what, 10 lines of code? Simple code, too. It should be obvious. I score for that.

(2) I don't recall ever (EVER) having to write a switch statement where I wanted to leave out a break statement so that the cases could share code. Never. I do, however, recall numerous times when I forgot to put in the break statements which should have been there. So I score for that feature too. It should have been the other way around: put in a "continue" statement if you want the current case to continue to the next case's code.
 
Hot dog! An advertiser loves us THIS much:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic