'a'..'z'

C

cruster

Is it possible to achieve something like this?

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

Luc The Perverse

cruster said:
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 :)
 
B

Bart Cremers

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
 
S

Stefan Ram

cruster said:
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( '!' ); }}
 
C

Chris Uppal

cruster said:
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
 
J

Jeffrey Schwab

cruster said:
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...
}
 
C

Chris Uppal

Mark Space wrote:

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

Luc The Perverse

Dale King said:
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.
 
D

Dale King

Luc said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,486
Latest member
Deanna5597

Latest Threads

Top