Switch statement for strings

D

David Segall

Is there a popular idiom for a switch statement that takes a string? I
want to emulate something roughly like:

switch (myString) {
case "Apr":
case "Jun":
case "Sep":
case "Nov":
System.out.println("30 days");
break;
case "Feb":
// We need some extra information here
break;
default:
System.out.println("31 days");
}
 
L

Lasse Reichstein Nielsen

David Segall said:
Is there a popular idiom for a switch statement that takes a string?

Lots of if-else-if's would do it.

A better approach would be to not use strings to represent meaningfull
values. Parse them into a real representation of the concept you are
working with once and for all, instead of having to read through the
string every time it is used.

In this example, you are using three letter strings to represent
months. Instead, you could have a Month enum, or you could use a
number. Either of these can be used in a switch (but with an enum,
it would be better to have the objects know the month's length
itself).

/L
 
S

Stefan Ram

David Segall said:
Is there a popular idiom for a switch statement that takes a string?
I want to emulate something roughly like:
switch (myString) { case "Apr":

You might want to learn about perfect hash functions
Then calculate a perfect hash value for the string
variable and the constant strings.

(A normal hash function should also do, but »myString.hash()«
might return different hash values for different Java
implementations, so a specific hash function should be used.)
 
S

Stefan Ram

(A normal hash function should also do, but »myString.hash()«

I meant »myString.hashCode()«. But you still would have to use
»equals« unless it is known that the value of the string is
restricted to a small known set (i.e., the 12 months), so it
might be better to use a hash map instead if the value is
not restricted.

A direct translation would be:

map.put( "Apr", new java.lang.Runnable()
{ public void run(){ System.out.println("30 days"); }});

But it might be better to use:

map.put( "Apr", 30 );
 
B

Boris Stumm

David said:
Is there a popular idiom for a switch statement that takes a string? I
want to emulate something roughly like:

switch (myString) {
case "Apr":
case "Jun":
case "Sep":
case "Nov":
System.out.println("30 days");
break;
case "Feb":
// We need some extra information here
break;
default:
System.out.println("31 days");
}

You can try Enums:
enum month { Jan, Feb, ...};

and then:
switch(Month.valueOf("XXX")) {
case Jan:
case Feb:
...
}
 
S

Stefan Ram

David Segall said:
Is there a popular idiom for a switch statement that takes a string? I
want to emulate something roughly like:
switch (myString) {
case "Apr":

This might not be popular this way, but might allow a fast and small
switch table. It uses a perfect hash for "Jan" .. "Dec".

public class Main
{
int[] v;

public Main()
{ v = new int[ 256 ]; for( int i = 0; i < 256; ++i )v[ i ]=15;
v[ 97 ]= 0; v[ 98 ]= 9; v[ 99 ]= 4; v[ 101 ]= 2; v[ 103 ]= 9;
v[ 108 ]= 8; v[ 110 ]= 2; v[ 111 ]= 4; v[ 112 ]= 3; v[ 114 ]= 1;
v[ 116 ]= 3; v[ 117 ]= 1; v[ 118 ]= 4; v[ 121 ]= 0; }

int hash( final java.lang.String s )
{ return v[ s.charAt( 1 )]+v[ s.charAt( 2 )] ; }

void show( final java.lang.String s )
{ switch ( hash( s ))
{ case 4: case 3: case 5: case 8:
java.lang.System.out.println( 30 ); break;
case 11: break;
default: java.lang.System.out.println( 31 ); break; }}

public static void main( final java.lang.String[] args )
{ new Main().show( "Jun" ); new Main().show( "Oct" ); }}

It prints:

30
31
 

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
474,261
Messages
2,571,041
Members
48,769
Latest member
Clifft

Latest Threads

Top