You can only switch on integer constants (static final int) or literals.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
like I said, you can't...
You could create a Map with those Strings as keys and integer values indexing an array of integer constants as values which you can then use as indices for the switch.
Easier to use a series of if ("black".equals(blah)) ... instead.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
That MIGHT work but doesn't have to.
It all depends on the implementation of the hashcode method, which isn't guaranteed to return a unique number for each possible input.
So more than one String can yield the same hashcode.
And even if it works it's a very dirty hack.
Use enums instead.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
The odds are impossible to gauge without a thorough analysis of the algorithm used.
It is however extremely easy to provide a technically correct hashCode implementation that ALWAYS returns the exact same value, so you should NEVER rely on hashCode to be even remotely unique.
As to Enums, read up on those. You can switch on them as well as on integer constants.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
I repeat: You CAN'T use Strings.
So you MUST use either Enums or integer literals.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Well I think what you can do is you can use string operation on your input to get such a Switch Case Statement
For Example if you want to go for Black, blue or something else.....what you can do is you make user enter string and find the alphabet position where every string has a different Char
For Example
Black
Blue
Here if you take out 3rd char and Put Switch Case statement for 3rd Char
Case 'c':
Means Input was Black
Case 'u':
Means input string was Blue...
I think it can be done in this way but I am not sure ....Can anyone tell me is it possible or not
staneja
Junior Poster in Training
64 posts since Dec 2006
Reputation Points: 10
Solved Threads: 2
Only way I know how to emulate a switch on strings, and this has already been mentioned without example code, is like the following:
void StringSwitch (String str) {
if (str == null) { /* If the String is null... */ }
else if (str.equals ("Some Possibility")) { /* ... */ }
else if (str.equals ("Another Possibility")) { /* ... */ }
else if (str.equals ("Possibility # 3")) { /* ... */ }
// Etc, etc, etc
else { /* DEFAULT Operation */ }
}
Keep in mind that the checks are all performed, in order, until a match is made. With large "switch" statements, the more common values should be placed near the beginning, and less common values can come near the end of the if-else statements. That'll speed up code execution, because common values won't have to be checked against dozens of not-so-common values before a match is found.
Cudmore
Junior Poster in Training
74 posts since Nov 2005
Reputation Points: 20
Solved Threads: 6
That's not a switch at all, it's just a massive if-else statement pushed into a method (not a bad idea per se) that you called "switch".
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337