aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes switch statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "switch statement" Watch "switch statement" New topic
Author

switch statement

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i have an actionPerformed() that do to additions now has 7 cases. i tried to change it to a switch statement but ran into a problem(i thought i might)

the old code


the new code



is there any way to make the switch statement work or will i have to stick with else if?

oops, i forgot the break statements in my cases. but it won't matter if i can't use switch will it?


SCJP
Visit my download page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Just like the case labels needed to be compile time constants in "old" integer / enum switches, the case labels need to be compile time constants for String switches too. The problem is with view.getMessage().endsWith("Would you like to play again?"); that's a) not a compile time constant, and b) not a String.

What you can do is use a switch up to that "case", then use a default case for the remainder and an if-else-if-else inside that default case. It's either that, or keep the larger if-else-if-else block.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

that's what i thought. i like the first idea since the if else is getting pretty ugly. too bad i have to check for view.getMessage().endsWith("Would you like to play again?") before the last two cases.

BTW i noticed that using Strings in switch statements is new to java 7
and everyone is saying there is nothing worthwhile in it
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I don't do GUIs, so I may be just blowing smoke here, but this looks like a major red flag to me:



I'd think there would be a better way to detect what's happening that comparing Strings intended for humans. (This goes for the comparisons to the constants as well.)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

If I can make a suggestion - the best solution to this is not to use either if/then or a switch statement. The problem is that you're using the same ActionListener to respond to lots of different events. It seems to be commonly used in lots of examples, but I think it's a really poor design. A much more object-oriented solution is to have a different ActionListener for each event. That way, you don't have to do any checking within the methods at all.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

jeff, the reason i have to make that comparison is because the yes and no buttons mean something different in that context than they do the rest of the time. as for the constants, i could have just used string literals like "Yes" and "Begin" but the Constants class is a part of the MVC framework i am using.

anyway i found a pretty good solution

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Agree with Matthew Brown. I think this discussion would sit better on our Swing forum, so I shall move it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch statement