This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Java in General and the fly likes switch on String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "switch on String" Watch "switch on String" New topic

switch on String

vitesse wei
Ranch Hand

Joined: Sep 07, 2007
Posts: 100
Hi,I have two string variables,they could have 20x5 combinations,how can i implement the code to check on specific combination,"if else" is not good design,I can not use "switch" on string,"enum" is not good too,what is the better solution for this?

SCJP 5.0<br />SCWCD1.4<br />SCBCD5
Joe Ess

Joined: Oct 29, 2001
Posts: 9138


[How To Ask Questions On JavaRanch]
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
If the action is same for all the string matches then populate them in a static string list and use contains.

Sunil Kumar
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
If the actions are different use a hashmap to define the actions for each type of match

Note : These are just opinions, others may vary
[ December 29, 2008: Message edited by: Sunil Kumar ]
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: switch on String
It's not a secret anymore!