Results 1 to 15 of 15
Thread: switch on a string
- 02-14-2010, 01:34 PM #1
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
-
- 02-14-2010, 04:46 PM #3
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
thats the problem.
but is there another solution except if,else if, else if ,...?
- 02-14-2010, 04:57 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
-
enums, perhaps? Or perhaps a HashMap<String, YourInterface>, although the latter would depend on the hashcode of the String again.
I've heard of talk of java 7 allowing switch statements with String, but don't know for sure. Let's see if anyone more expert knows more.
-
- 02-14-2010, 05:27 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Well, there are a few proposals, this is one of them; imho they don't buy you much except for a bit of syntactic sugar.
kind regards,
Jos
- 02-15-2010, 09:09 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 02-15-2010, 09:16 AM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Why do you need this switch on strings?
A redesign of your solution might also help. e.g using command pattern for your action/events. Just thought that if you explain the problem you might be guided to a better solution.Last edited by r035198x; 02-15-2010 at 09:25 AM.
- 02-15-2010, 09:18 AM #10
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
but still, your not garanteed that it will be the right string he selected,isn't it?
It would be a nasty bug to track down.
there is no possibility other then using else if?
Most things in java have the source code in the src.zip file, is the switch statement scripted over there?(which would be strange because switch isn't an object and probably isn't there)
sorry for any spelling mistakes, I am not a native speaker
- 02-15-2010, 09:32 AM #11
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
[qoute]
but still, your not garanteed that it will be the right string he selected,isn't it?
It would be a nasty bug to track down.
there is no possibility other then using else if?
Most things in java have the source code in the src.zip file, is the switch statement scripted over there?(which would be strange because switch isn't an object and probably isn't there)
sorry for any spelling mistakes, I am not a native speaker
[/qoute]
If you use the Map like suggested then you don't need the else if.
Just something like
Java Code:String testValue = ...; Runnable someCode = map.get(testValue);//returns the Runnable mapped to testValue someCode.run();//or thread start it
- 02-15-2010, 12:06 PM #12
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
my problem:
String prefix;
switch(prefix){
case "server-message":
case "broadcast-message":
case "private-message":
...
But I heard hash codes aren't identicall so I am asking if there's another possibiltiy
- 02-15-2010, 12:15 PM #13
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Did you read replies that are suggesting that you use a map of Runnables?
- 02-15-2010, 10:38 PM #14
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
yes,I did.
I was asking for other posibilities
- 02-16-2010, 10:31 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Without actually knowing what it is you're doing we're not necessarily going to give you the best answer, though you've been given a good option above with the Map/Runnable.
If you're switching on this prefix to decide how to process the message I'd suggest having different message types represented by different Message subclasses, but you'd still have to actually create the Message, which would still involve looking at that prefix String.
Similar Threads
-
switch
By dj kourampies in forum New To JavaReplies: 17Last Post: 01-30-2009, 05:32 PM -
switch
By dj kourampies in forum New To JavaReplies: 2Last Post: 01-30-2009, 08:46 AM -
Switch help please!!!!
By soc86 in forum New To JavaReplies: 6Last Post: 11-23-2008, 07:25 PM -
How to use Switch keyword
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:07 PM -
Help with Switch string code
By toby in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks