•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 401,669 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,605 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 69009 | Replies: 16
![]() |
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
•
•
Join Date: Oct 2004
Location: San Francisco, CA
Posts: 338
Reputation:
Rep Power: 4
Solved Threads: 2
•
•
Join Date: Dec 2004
Posts: 12
Reputation:
Rep Power: 4
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: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Rep Power: 0
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");
}
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
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.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Rep Power: 0
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 ?
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation:
Rep Power: 18
Solved Threads: 195
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.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
•
•
Join Date: Dec 2006
Posts: 3
Reputation:
Rep Power: 0
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));
}
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)
Other Threads in the Java Forum
- Previous Thread: Help
- Next Thread: Logical operations and data-checks



Linear Mode