User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Dec 2004
Posts: 12
Reputation: niamul is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
niamul niamul is offline Offline
Newbie Poster

Help switch statement on String in Java

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: switch statement on String in Java

  #2  
Dec 28th, 2004
You can only switch on integer constants (static final int) or literals.
Reply With Quote  
Join Date: Oct 2004
Location: San Francisco, CA
Posts: 338
Reputation: paradox814 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: switch statement on String in Java

  #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  
Join Date: Dec 2004
Posts: 12
Reputation: niamul is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
niamul niamul is offline Offline
Newbie Poster

Re: switch statement on String in Java

  #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  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: switch statement on String in Java

  #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  
Join Date: Dec 2006
Posts: 3
Reputation: cyberowl is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cyberowl cyberowl is offline Offline
Newbie Poster

Re: switch statement on String in Java

  #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  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: switch statement on String in Java

  #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.
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.
Reply With Quote  
Join Date: Dec 2006
Posts: 3
Reputation: cyberowl is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cyberowl cyberowl is offline Offline
Newbie Poster

Re: switch statement on String in Java

  #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  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: switch statement on String in Java

  #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.
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.
Reply With Quote  
Join Date: Dec 2006
Posts: 3
Reputation: cyberowl is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cyberowl cyberowl is offline Offline
Newbie Poster

Re: switch statement on String in Java

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 7:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC