f



'a'..'z'

Is it possible to achieve something like this?

switch (mystring.charAt(0)) {
case 'a'..'z':
// do something
break;
}

0
cruster (2)
6/26/2006 11:02:12 AM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

11 Replies
1893 Views

Similar Articles

[PageSpeed] 7

"cruster" <cruster@gmail.com> wrote in message 
news:1151319731.988814.326200@m73g2000cwd.googlegroups.com...
> Is it possible to achieve something like this?
>
> switch (mystring.charAt(0)) {
> case 'a'..'z':
> // do something
> break;
> }
>

There are times when an if statement may be more appropriate ;)

Sorry - java is not VB :)

--
LTP

:) 


0
6/26/2006 11:30:27 AM
cruster schreef:

> Is it possible to achieve something like this?
>
> switch (mystring.charAt(0)) {
> case 'a'..'z':
> // do something
> 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

0
bcremers (150)
6/26/2006 11:36:31 AM
Luc The Perverse wrote:
> "cruster" <cruster@gmail.com> wrote in message 
> news:1151319731.988814.326200@m73g2000cwd.googlegroups.com...
> 
>>Is it possible to achieve something like this?
>>
>>switch (mystring.charAt(0)) {
>>case 'a'..'z':
>>// do something
>>break;
>>}
>>
> 
> 
> There are times when an if statement may be more appropriate ;)
> 
> Sorry - java is not VB :)

Or perl ;-)

    BugBear
0
bugbear (609)
6/26/2006 11:40:38 AM
"cruster" <cruster@gmail.com> writes:
>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( '!' ); }}

0
ram (2986)
6/26/2006 11:55:45 AM
cruster wrote:
> Is it possible to achieve something like this?
>
> switch (mystring.charAt(0)) {
> case 'a'..'z':
> // do something
> 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


0
chris.uppal (3980)
6/26/2006 12:17:11 PM
cruster wrote:
> Is it possible to achieve something like this?
> 
> switch (mystring.charAt(0)) {
> case 'a'..'z':
> // do something
> 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...
	}
0
jeff34 (1597)
6/26/2006 3:09:26 PM
Chris Uppal wrote:

> 
> 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...
0
markspace1 (668)
6/26/2006 11:35:31 PM
Mark Space wrote:

[me:]
> > 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...

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


0
chris.uppal (3980)
6/27/2006 11:04:44 AM
Luc The Perverse wrote:
> "cruster" <cruster@gmail.com> wrote in message 
> news:1151319731.988814.326200@m73g2000cwd.googlegroups.com...
>> Is it possible to achieve something like this?
>>
>> switch (mystring.charAt(0)) {
>> case 'a'..'z':
>> // do something
>> break;
>> }
>>
> 
> 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).

-- 
  Dale King
0
DaleWKing2 (33)
6/28/2006 5:54:58 AM
"Dale King" <DaleWKing@gmail.com> wrote in message 
news:dradnZ6PWr-chz_ZnZ2dnUVZ_oqdnZ2d@insightbb.com...
> Luc The Perverse wrote:
>> "cruster" <cruster@gmail.com> wrote in message 
>> news:1151319731.988814.326200@m73g2000cwd.googlegroups.com...
>>> Is it possible to achieve something like this?
>>>
>>> switch (mystring.charAt(0)) {
>>> case 'a'..'z':
>>> // do something
>>> break;
>>> }
>>>
>>
>> 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).

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

:) 


0
6/28/2006 6:16:12 AM
Luc The Perverse wrote:
> "Dale King" <DaleWKing@gmail.com> wrote in message 
> news:dradnZ6PWr-chz_ZnZ2dnUVZ_oqdnZ2d@insightbb.com...
>> Luc The Perverse wrote:
>>> "cruster" <cruster@gmail.com> wrote in message 
>>> news:1151319731.988814.326200@m73g2000cwd.googlegroups.com...
>>>> Is it possible to achieve something like this?
>>>>
>>>> switch (mystring.charAt(0)) {
>>>> case 'a'..'z':
>>>> // do something
>>>> break;
>>>> }
>>>>
>>> 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).
> 
> 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.

-- 
  Dale King
0
DaleWKing2 (33)
7/5/2006 6:18:37 PM
Reply: