wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes switch on a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "switch on a string?" Watch "switch on a string?" New topic
Author

switch on a string?

Pat Peg
Ranch Hand

Joined: Feb 04, 2005
Posts: 194
I am looking for a work around that would alow me to do a switch when I am given a string. Has anyone been able to do this?
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

If they are single character strings, you could use CHAR although thats rare.

Usually I assign a "public static final int" for each type of output I expect, basically defining a category, then use this inside my switch statements. There is still the problem of if a user inputs a string and you need to convert it to int although you could do this efficiently with a static HashMap.
[ November 01, 2005: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
The short answer is you can't! The expression in "switch(expr)" must be
of type int. But your options include:
* using if statements -- how many cases are there, anyway?
* using a Map<String, ActionInThisCase>, for example.
* converting the string to a unique int and switching on that;
for instance if all that matters is the first char, you can do a switch on
its unicode value. This would miss some invalid strings, however.


There is no emoticon for what I am feeling!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
If there is a small, fixed number of strings, perhaps what you actually have
here is a enumerated type:

Enums guide -- Java 1.5
Preetham Chandrasekhar
Ranch Hand

Joined: Nov 05, 2003
Posts: 98
since we are using enums here...I modified the code by Jeff to use more of enums:


public enum Fruit {

APPLE(1,"green"), BERRY(2,"red"), CHERRY(3,"dark red"), DATE(4,"brown");
private final int num;
private final String str;

Fruit(int num, String str) {
this.num = num;
this.str = str;
}

public int getNum() {
return num;
}

public String getStr() {
return str;
}

public static String getStringFromNum(int num) {
for(Fruit fruit : Fruit.values()) {
if(fruit.num == num) {
return fruit.getStr();
}
}
return null;
}

public static void main(String args[]) {
Fruit fruit = Fruit.valueOf("APPLE");
String s = "???";
s =Fruit.getStringFromNum(fruit.getNum());
System.out.println("the value of the string is: " + s);
}

}


"In theory, there is no difference between theory and practice. But, in practice, there is."<br /> - Jan L.A. van de Snepscheut
Pat Peg
Ranch Hand

Joined: Feb 04, 2005
Posts: 194
The possible number of unique strings could be 2 to 5 dozen. The possible number of switches would be 2 dozen or less because, in some cases, multiple strings map to the same output. Because of the large nmber is the exact reason I wanted to implement switch. currently I am 'if else' in my test case which is only limited to about 5 or 6 possible strings. When it grows it may become unmanagable.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

You could define equivalences among different strings that would simplify your logic, especially if you convert the strings into equivalence classes of ints.

Also, you mentioned manageability, you throw a database into the mix in order to organize dozens to hundreds of possible types, but this is likely beyond the scope of what you are doing.
Preetham Chandrasekhar
Ranch Hand

Joined: Nov 05, 2003
Posts: 98
then I would really suggest you to use the features in enum and by using them u can avoid the switch...thatn logic is part of the example i posted here
Pat Peg
Ranch Hand

Joined: Feb 04, 2005
Posts: 194
I'm sorry, I haven't had much xp with enums. Is Fruit suppose to be a class or a method in your example?
Pat Peg
Ranch Hand

Joined: Feb 04, 2005
Posts: 194
Wouldn't a HashTable work just as well?
Preetham Chandrasekhar
Ranch Hand

Joined: Nov 05, 2003
Posts: 98
Fruit is an enum in my example....and yes...it is a class as well...the Fruit method there is the constructor which takes in 2 arguments....the value and the string which is the result u are interested.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Pat Peg:
I'm sorry, I haven't had much xp with enums.


If you are using the current version of Java (1.5), then I strongly recommend
you check out the New Enum Feature.
For example, I've been writing a Sudoku-playing app, and I originally
used an int to hold one of the digits 1-9. I switched over to an enum
and I've never looked back: no need to do bounds checking on an int,
and more importantly, I get to use the handy java.util collections EnumSet and EnumMap.

Anyway, Preetham and I are on the same page on enums. In my example I
demonstrated a simple switch, and he showed how to add properties to
an enum to avoid writing that switch. With enums, it's good to know
how to do both, since sometimes the switch is inevitable, say when you
don't have permission to edit the enum class further.

Preetham also added a num attribute, but if you're happy with the
offsets in the enum's value array, you don't have to do that:

Another technique that is useful to keep in mind is that with each
value in the enum, you can subclass on the fly and override:

In the above code I found it a bit mysterious that Fruit2 has the abstract
method getComplementaryFruit, but I had to declare "public enum Fruit2",
not "public abstract enum Fruit2" , but it compiles.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch on a string?