wood burning stoves
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

Case switch

frank van schie

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

Paul Clapham

Joined: Oct 14, 2005
Posts: 19848

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

Joined: Oct 13, 2005
Posts: 45276
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

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

Joined: Sep 20, 2010
Posts: 4199

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!