This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 13, 2005
Posts: 38033
    
  22
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: 3599
    
  14

The code would probably be easier to read if you used enum constants:
 
Consider Paul's rocket mass heater.
 
subject: Case switch
 
Similar Threads
switch case
Decoding Expresssions to values same as 'DECODE' function in Oracle
Is switchcase is better than if else
switch case
Nesting My If statements