Results 1 to 15 of 15
  1. #1
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default switch on a string

    I know java doesn't support switching on a String.
    Is this allowed?:
    Java Code:
    switch("lol".hashCode()){
    case "funny".hascode:
    ...
    is this seen as a bad way,...?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dinosoep View Post
    I know java doesn't support switching on a String.
    Is this allowed?:
    Java Code:
    switch("lol".hashCode()){
    case "funny".hascode:
    ...
    is this seen as a bad way,...?
    I don't trust it. How are you guaranteed that the hashcode will be unique?

  3. #3
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    thats the problem.
    but is there another solution except if,else if, else if ,...?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,449
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dinosoep View Post
    thats the problem.
    but is there another solution except if,else if, else if ,...?
    You could construct a Map<String, Runnable> where the Runnable contains the code to run for a String. A bit of fiddling would solve the switch "fall through" feature.

    kind regards,

    Jos

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    You could construct a Map<String, Runnable> where the Runnable contains the code to run for a String. A bit of fiddling would solve the switch "fall through" feature.
    Ah, I was a bit slow in my reply, but as I noted above, the Map would respond to the hash of the String, so I too wonder how this is better than the switch statement on the String's hashcode.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,449
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Ah, I was a bit slow in my reply, but as I noted above, the Map would respond to the hash of the String, so I too wonder how this is better than the switch statement on the String's hashcode.
    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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    Ah, I was a bit slow in my reply, but as I noted above, the Map would respond to the hash of the String, so I too wonder how this is better than the switch statement on the String's hashcode.
    Um, no. The Map uses the hashcode to narrow down the list of possible things (assuming it's a HashMap, of course). It then uses equals on those possibilities to find the actual mapped result.

    So it's definitely better than the switch on a hash code.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  10. #10
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    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

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    [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

  12. #12
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    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

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Did you read replies that are suggesting that you use a map of Runnables?

  14. #14
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    yes,I did.
    I was asking for other posibilities

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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

  1. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 05:32 PM
  2. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 08:46 AM
  3. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 07:25 PM
  4. How to use Switch keyword
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:07 PM
  5. Help with Switch string code
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •