Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch on a string?

 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}

}
 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't a HashTable work just as well?
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic