Reputation: 1322
I wanted to find a way to do this in java 6, but it doesn't exist:
switch (c) {
case ['a'..'z']: return "lower case" ;
There was a proposal to add this to the java language some time ago: http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000213.html, has anything materialized in java 7?
What are other ways to rewrite this code in java 6, that would read more like a switch/case:
if (theEnum == MyEnum.A || theEnum == MyEnum.B){
}else if(), else if, else if...
Upvotes: 0
Views: 5724
Reputation: 6130
The simplest thing would be:
if (Character.isLowerCase(c)){
return "lowercase";
}
Which will also work with á ö and the sort
Upvotes: 2
Reputation: 160291
Or:
if (inRange(c, 'a', 'z')) {
...
}
or use a regex like normal, or a map, or...
With regards to your enum expression, it depends on what you're actually doing, but it might just be a map with implementations or values.
Upvotes: 0
Reputation: 13863
To the first part, one options for strings
if(c.equals(c.toLowerCase())) return "lower case";
To the second part, you can use switch with enums....
switch(theEnum){
case A:
case B:
break;
case C:
break;
...
}
Upvotes: 0
Reputation: 1145
You could do something like:
switch (c) {
case 'a':
case 'b':
case 'c':
//...
doSomething();
break;
}
Upvotes: 2