switch statement on String in Java

Reply

Join Date: Dec 2004
Posts: 12
Reputation: niamul is an unknown quantity at this point 
Solved Threads: 1
niamul niamul is offline Offline
Newbie Poster

switch statement on String in Java

 
0
  #1
Dec 28th, 2004
I am a new programmer in Java and while programming I faced the following problem. I am trying to use switch statement on String but it gives compilation error. If any one suggest me how to use String in switch-case statement , it will be helpful to me.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: switch statement on String in Java

 
0
  #2
Dec 28th, 2004
You can only switch on integer constants (static final int) or literals.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: switch statement on String in Java

 
0
  #3
Dec 28th, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: niamul is an unknown quantity at this point 
Solved Threads: 1
niamul niamul is offline Offline
Newbie Poster

Re: switch statement on String in Java

 
0
  #4
Dec 28th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: switch statement on String in Java

 
0
  #5
Dec 28th, 2004
like I said, you can't...
You could create a Map with those Strings as keys and integer values indexing an array of integer constants as values which you can then use as indices for the switch.
Easier to use a series of if ("black".equals(blah)) ... instead.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: cyberowl is an unknown quantity at this point 
Solved Threads: 0
cyberowl cyberowl is offline Offline
Newbie Poster

Re: switch statement on String in Java

 
0
  #6
Dec 9th, 2006
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");
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: switch statement on String in Java

 
0
  #7
Dec 10th, 2006
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: cyberowl is an unknown quantity at this point 
Solved Threads: 0
cyberowl cyberowl is offline Offline
Newbie Poster

Re: switch statement on String in Java

 
0
  #8
Dec 10th, 2006
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 ?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: switch statement on String in Java

 
0
  #9
Dec 10th, 2006
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 3
Reputation: cyberowl is an unknown quantity at this point 
Solved Threads: 0
cyberowl cyberowl is offline Offline
Newbie Poster

Re: switch statement on String in Java

 
0
  #10
Dec 10th, 2006
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));
}
}
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC