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 ]
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
posted
0
If there is a small, fixed number of strings, perhaps what you actually have here is a enumerated type:
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: 188
posted
0
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.
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
posted
0
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: 188
posted
0
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: 188
posted
0
Wouldn't a HashTable work just as well?
Preetham Chandrasekhar
Ranch Hand
Joined: Nov 05, 2003
Posts: 98
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.