jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Case switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Case switch" Watch "Case switch" New topic
Author

Case switch

frank van schie
Greenhorn

Joined: May 16, 2011
Posts: 7
How do i make a case switch react to a string type instead of an integer?

Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19719
    
  10

By waiting for Java 7, which supports that, to be released.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43900
    
  32
You could try a switch on the hashCode of the String, but remember you have to work out the values after the case keyword before compiling; what follows "case" must be a compile-time constant.
Jasper Brown
Greenhorn

Joined: May 31, 2011
Posts: 3
I needed to do this for a project and ended up using a long chain of if, else if ... statements. Very messy but it worked fine.

looked like

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3983
    
  18

The code would probably be easier to read if you used enum constants:


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Case switch
 
It's not a secret anymore!