Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch statement

 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Matthew Brown. I think this discussion would sit better on our Swing forum, so I shall move it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic