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 391,587 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 2,644 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: 67176 | Replies: 16
Reply
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
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: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: switch statement on String in Java

  #11  
Dec 10th, 2006
I repeat: You CAN'T use Strings.
So you MUST use either Enums or integer literals.
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: 2
Reputation: rLh is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rLh rLh is offline Offline
Newbie Poster

Re: switch statement on String in Java

  #12  
Dec 13th, 2006
where did u find your sampl about switches on mathematical operation?
Reply With Quote  
Join Date: Dec 2006
Posts: 1
Reputation: Ficke is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Ficke Ficke is offline Offline
Newbie Poster

Re: switch statement on String in Java

  #13  
Dec 13th, 2006
Originally Posted by jwenting View Post
I repeat: You CAN'T use Strings.
So you MUST use either Enums or integer literals.


Well, of course you can't use Strings directly, but with the use of default enum mappings you can get nearly exactly the same thing. Looking at this:

http://www.xefer.com/2006/12/switchonstring

Enum.valueOf() is using a hash map to lookup values by the actual name of the enum, so effectively this is doing what any other language that supports switches on string is doing under the hood.

This is certainly better than using a cascade of if/else statements or ginning up a hash implementation from scratch.
Reply With Quote  
Join Date: Dec 2006
Posts: 54
Reputation: staneja is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
staneja's Avatar
staneja staneja is offline Offline
Junior Poster in Training

Re: switch statement on String in Java

  #14  
Dec 14th, 2006
Well I think what you can do is you can use string operation on your input to get such a Switch Case Statement

For Example if you want to go for Black, blue or something else.....what you can do is you make user enter string and find the alphabet position where every string has a different Char

For Example

Black
Blue
Here if you take out 3rd char and Put Switch Case statement for 3rd Char

Case 'c':
Means Input was Black

Case 'u':
Means input string was Blue...



I think it can be done in this way but I am not sure ....Can anyone tell me is it possible or not
Reply With Quote  
Join Date: Dec 2006
Posts: 3
Reputation: manojdesai is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
manojdesai manojdesai is offline Offline
Newbie Poster

Re: switch statement on String in Java

  #15  
Dec 14th, 2006
i can help u! Just leaving m know i can assist u later. ok . fine?
Reply With Quote  
Join Date: Nov 2005
Posts: 72
Reputation: Cudmore is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Cudmore's Avatar
Cudmore Cudmore is offline Offline
Junior Poster in Training

Solution Re: switch statement on String in Java

  #16  
Dec 28th, 2006
Only way I know how to emulate a switch on strings, and this has already been mentioned without example code, is like the following:

  1. void StringSwitch (String str) {
  2. if (str == null) { /* If the String is null... */ }
  3. else if (str.equals ("Some Possibility")) { /* ... */ }
  4. else if (str.equals ("Another Possibility")) { /* ... */ }
  5. else if (str.equals ("Possibility # 3")) { /* ... */ }
  6. // Etc, etc, etc
  7. else { /* DEFAULT Operation */ }
  8. }

Keep in mind that the checks are all performed, in order, until a match is made. With large "switch" statements, the more common values should be placed near the beginning, and less common values can come near the end of the if-else statements. That'll speed up code execution, because common values won't have to be checked against dozens of not-so-common values before a match is found.
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
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: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: switch statement on String in Java

  #17  
Dec 29th, 2006
That's not a switch at all, it's just a massive if-else statement pushed into a method (not a bad idea per se) that you called "switch".
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  
Reply

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

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

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