Perry Smith

Java Questions & Answers

Home / Java Q & A Page 2 / Java Q & A Archive Main Page

Java: Switch on a String object
Wednesday, 11-Nov-98 18:45:42
194.82.103.21 writes:

Can anyone provide step by step instructions on how to perform a switch
statement on a string variable. I presume you would use the
getChars()/getByte() methods, but I have no idea how. Help please!

Responses
Re: Java: Switch on a String object
Friday, 13-Nov-98 03:45:33
171.212.202.212 writes:

The only thing I can suggest is that you create some constants like
"static final int STRING1 = 1" or something like that instead of
assigning a string to the switch variable, since I believe it will only
accept an integer as the switch variable.

MikeD

Re: Java: Switch on a String object
Friday, 13-Nov-98 13:51:31
148.5.110.65 writes:

Another option for you to try is to switch on the hashCode for the the
string. I tried a couple and they seem to be unique for a given string
(case sensitivity included). I don't know how this would work if you are
worried about internationallization. One draw back to this is that you
need to first determine the hashCode for all of your possible strings.

Scott_M2

Home / Java Q & A Page 2 / Java Q & A Archive Main Page