Reputation: 7076
I'm trying to simplify a Card class, and am wondering if there's any way to use a range of values in a switch statement?
CONSIDER:
if((card<14))
suit="Hearts";
else
if((card>14)&&(card<27))
suit="Clubs";
etc.
Instead I'd like to use a switch statement, such as:
switch(card){
case1: through case13:
suit="Hearts";
break;
etc.
I wasn't able to find anything in the Java Tutorials that suggest there is such a variation of switch. But is there?
Upvotes: 4
Views: 9094
Reputation: 70929
This was proposed in Project Coin some time back.
Basically the switch operator now operates on built-ins and Objects. While a well defined range exists for selecting two built-ins, switches that are operating on Objects don't have a well defined range. Currently two classes of objects are supported, Strings
and Enum
.
Is "car" between "A" and "D"? Depends on how you like to handle case sensitivity.
Is MyEnum.ONE
before MyEnum.TWO
? Depending on it in production code is a very bad idea (as Josh Bloch would love to explain), and if you need an ordering, the maintainable way is to implement a non-index bound comparator. This much better practice would be hard to integrate into a simple switch without forcing every enum to implement an ordering (which doesn't make sense for enums that are not ordered).
The project coin proposal is found here.
Josh Bloch's excellent presentation on why not to rely on implicit enum ordering is found within the presentation here.
Upvotes: 1
Reputation: 7064
Some libraries provide the "Range" feature. It will not work with the switch syntax but it should do the job.
For example, the Range class of Guava.
Range hearts = Ranges.closed(1, 13);
Range clubs = Ranges.closed(14, 17);
// ...
if (hearts.contains(value)) {
// case heart...
} else if (clubs.contains(value) {
// case club...
} // ...
Upvotes: 0
Reputation: 55449
About the best you can do is something like this (see below). So in some cases (no pun intended :)), it's just better to use an if statement.
switch(card){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
suit="Hearts";
break;
}
However, another approach you could consider is using a map.
Map<Integer, String> map = new HashMap<Integer, String>();
for (int i = 1; i <= 14; ++i) {
map.put(i, "Hearts");
}
for (int i = 15; i <= 26; ++i) {
map.put(i, "Clubs");
}
Then you can look up the card's suit using the map.
String suit1 = map.get(12); // suit1 will be Hearts after the assignment
String suit2 = map.get(23); // suit2 will be Clubs after the assignment
Upvotes: 8
Reputation: 5123
This is about the best you're going to get:
switch(card){
case1:
case2:
case3:
suit="Hearts";
break;
Upvotes: 0
Reputation: 49197
Java doesn't allow you to do any such thing unfortunately. Other JVM languages might though, i.e., Scala.
Upvotes: 3