| | |
switch statement on String in Java
![]() |
you would have to covert the string to char
and you would use single quotes, example:
case 'x':
i think this kinda blows too but what are you going to do?
and you would use single quotes, example:
case 'x':
i think this kinda blows too but what are you going to do?
•
•
Join Date: Dec 2004
Posts: 12
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by paradox814
you would have to covert the string to char
and you would use single quotes, example:
case 'x':
i think this kinda blows too but what are you going to do?
thank u paradox.
But my purpose is not a single character. My requirement is String such as "black" "blue" etc.
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Solved Threads: 0
Was doing a research about it too. Got a simple solution, don't know if it is the best.
The String Class has a hashCode method that return the String hashcode (int) so u can do as this:
String name = "Victor";
switch (name.hashcode()) {
case "Victor".hashCode() : System.out.println("Name is Victor");
break;
case "Paul".hashCode() : System.out.println("Name is Paul");
break;
default : System.out.println("Default");
}
The String Class has a hashCode method that return the String hashcode (int) so u can do as this:
String name = "Victor";
switch (name.hashcode()) {
case "Victor".hashCode() : System.out.println("Name is Victor");
break;
case "Paul".hashCode() : System.out.println("Name is Paul");
break;
default : System.out.println("Default");
}
That MIGHT work but doesn't have to.
It all depends on the implementation of the hashcode method, which isn't guaranteed to return a unique number for each possible input.
So more than one String can yield the same hashcode.
And even if it works it's a very dirty hack.
Use enums instead.
It all depends on the implementation of the hashcode method, which isn't guaranteed to return a unique number for each possible input.
So more than one String can yield the same hashcode.
And even if it works it's a very dirty hack.
Use enums instead.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Solved Threads: 0
Said that I didn't know if it was the best. Two words can really return the same hashCode, but you agree that the odds of this happening are very exceptional, don't you?
You can do too a hashCode compare of the words choosed before.
Don't understand how you are thinking about using Enum. Can you explain more ?
You can do too a hashCode compare of the words choosed before.
Don't understand how you are thinking about using Enum. Can you explain more ?
The odds are impossible to gauge without a thorough analysis of the algorithm used.
It is however extremely easy to provide a technically correct hashCode implementation that ALWAYS returns the exact same value, so you should NEVER rely on hashCode to be even remotely unique.
As to Enums, read up on those. You can switch on them as well as on integer constants.
It is however extremely easy to provide a technically correct hashCode implementation that ALWAYS returns the exact same value, so you should NEVER rely on hashCode to be even remotely unique.
As to Enums, read up on those. You can switch on them as well as on integer constants.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Solved Threads: 0
Took me some time to understand how to do this with enum... but still didn't get how it can help with Strings.
Example that I found.
public enum Operation {
PLUS, MINUS, TIMES, DIVIDE;
// Do arithmetic op represented by this constant
double eval(double x, double y){
switch(this) {
case PLUS: return x + y;
case MINUS: return x - y;
case TIMES: return x * y;
case DIVIDE: return x / y;
}
throw new AssertionError("Unknown op: " + this);
}
}
Example using it:
public class Teste {
public static void main(String args[]) {
System.out.println(Operation.PLUS.eval(7,8));
System.out.println(Operation.MINUS.eval(7,8));
System.out.println(Operation.TIMES.eval(7,8));
System.out.println(Operation.DIVIDE.eval(7,8));
}
}
Example that I found.
public enum Operation {
PLUS, MINUS, TIMES, DIVIDE;
// Do arithmetic op represented by this constant
double eval(double x, double y){
switch(this) {
case PLUS: return x + y;
case MINUS: return x - y;
case TIMES: return x * y;
case DIVIDE: return x / y;
}
throw new AssertionError("Unknown op: " + this);
}
}
Example using it:
public class Teste {
public static void main(String args[]) {
System.out.println(Operation.PLUS.eval(7,8));
System.out.println(Operation.MINUS.eval(7,8));
System.out.println(Operation.TIMES.eval(7,8));
System.out.println(Operation.DIVIDE.eval(7,8));
}
}
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Comparing Images For Similarity
- Next Thread: Help with proper output display in GUI
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






