Is it possible to achieve something like this?
switch (mystring.charAt(0)) {
case 'a'..'z':
// do something
break;
}
Luc The Perverse - 26 Jun 2006 12:30 GMT
> Is it possible to achieve something like this?
>
[quoted text clipped - 3 lines]
> break;
> }
There are times when an if statement may be more appropriate ;)
Sorry - java is not VB :)
--
LTP
:)
bugbear - 26 Jun 2006 12:40 GMT
>>Is it possible to achieve something like this?
>>
[quoted text clipped - 7 lines]
>
> Sorry - java is not VB :)
Or perl ;-)
BugBear
Dale King - 28 Jun 2006 06:54 GMT
>> Is it possible to achieve something like this?
>>
[quoted text clipped - 5 lines]
>
> There are times when an if statement may be more appropriate ;)
And I don't think this is one of those cases! Switch statements should
support ranges and value lists. I submitted an RFE for such a feature
years ago:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4269827
Using a switch is more readable than the if-else and can actually be
much more efficient than if-else (see the comments I attached to the RFE).

Signature
Dale King
Luc The Perverse - 28 Jun 2006 07:16 GMT
>>> Is it possible to achieve something like this?
>>>
[quoted text clipped - 14 lines]
> Using a switch is more readable than the if-else and can actually be much
> more efficient than if-else (see the comments I attached to the RFE).
The listed example is basically an if statement. If there were even 1 or 2
other cases I would not have a problem with it.
--
LTP
:)
Dale King - 05 Jul 2006 19:18 GMT
>>>> Is it possible to achieve something like this?
>>>>
[quoted text clipped - 16 lines]
> The listed example is basically an if statement. If there were even 1 or 2
> other cases I would not have a problem with it.
I'm not sure what you mean by saying the example is basically an if
statement. The fact that it can only currently be expressed as an if
statement has no bearing on the matter.
Any switch statement can be recoded as a series of if statements. Yet
switch and if have different semantics.
The example I gave is a case where it is difficult to express the logic
using if statements in a way that is both readable and efficient.
Expanding cases to accept ranges and lists allow for a more readable
expression of the logic and the opportunity for the compiler to optimize
the comparisons.

Signature
Dale King
Bart Cremers - 26 Jun 2006 12:36 GMT
cruster schreef:
> Is it possible to achieve something like this?
>
[quoted text clipped - 3 lines]
> break;
> }
it's possible, only a bit ugly in Java:
switch (mystring.charAt(0)) {
case 'a':
case 'b':
...
case 'z:
// do something
break;
}
In this case you can of course go for an if test:
if (mystring.charAt(0) >= 'a' && mystring.charAt(0) <= 'z') {
// do something
}
Regards,
Bart
Stefan Ram - 26 Jun 2006 12:55 GMT
>case 'a'..'z':
public class Main
{
public static void test( final char c )
{ switch( c >= 'a' && c <= 'z' ? -1 : c )
{ case -1: java.lang.System.out.println( "a .. z" ); break;
case '!': java.lang.System.out.println( "exclamation mark" ); break;
default: java.lang.System.out.println( "unknown" ); break; }}
public static void main( final java.lang.String[] args )
{ test( '\u0060' ); test( 'a' ); test( 'b' ); test( 'y' );
test( 'z' ); test( '{' ); test( '!' ); }}
Chris Uppal - 26 Jun 2006 13:17 GMT
> Is it possible to achieve something like this?
>
[quoted text clipped - 3 lines]
> break;
> }
Yes. Easy:
switch (mystring.charAt(0))
{
case: 'a':
case: 'b':
case: 'c':
case: 'd':
case: 'e':
case: 'f':
case: 'g':
case: 'h':
case: 'i':
case: 'j':
case: 'k':
case: 'l':
case: 'm':
case: 'n':
case: 'o':
case: 'p':
case: 'q':
case: 'r':
case: 's':
case: 't':
case: 'u':
case: 'v':
case: 'w':
case: 'x':
case: 'y':
case: 'z': // do something
}
You may find
char c = mystring.charAt(0);
if (c >= 'a' && c <= 'z')
{
// do something
}
more expressive, though.
NB: if what you are really trying to do is characterise characters (identify
alphabetic characters, etc) then you'd almost certainly be better off
remembering that you are not working in ASCII but Unicode, and using the static
methods of class java.lang.Character, such as isLetter(int) and isLetter(char).
-- chris
Mark Space - 27 Jun 2006 00:35 GMT
> NB: if what you are really trying to do is characterise characters (identify
> alphabetic characters, etc) then you'd almost certainly be better off
> remembering that you are not working in ASCII but Unicode, and using the static
> methods of class java.lang.Character, such as isLetter(int) and isLetter(char).
Oo, good point O_o Something that's easy for C programmers to trip up on...
Chris Uppal - 27 Jun 2006 12:04 GMT
[me:]
> > NB: if what you are really trying to do is characterise characters
> > (identify alphabetic characters, etc) then you'd almost certainly be
[quoted text clipped - 4 lines]
> Oo, good point O_o Something that's easy for C programmers to trip up
> on...
Not to sound /too/ priggish, but even C programmers should know to use
isdigit() and so on in preference to hard-coding character classes.
;-)
-- chris
Jeffrey Schwab - 26 Jun 2006 16:09 GMT
> Is it possible to achieve something like this?
>
[quoted text clipped - 3 lines]
> break;
> }
You may want to write a method to categorize characters, and return an
integer constant or Enum to indicate the category of a given character.
E.g:
switch(categorize(myString.charAt(0)) {
case LOWERCASE_LETTER:
// do something
break;
// other cases...
}