•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,701 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,343 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: 69158 | Replies: 16
![]() |
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,700
Reputation:
Rep Power: 18
Solved Threads: 195
•
•
Join Date: Dec 2006
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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.
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
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
Only way I know how to emulate a switch on strings, and this has already been mentioned without example code, is like the following:
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.
java Syntax (Toggle Plain Text)
void StringSwitch (String str) { if (str == null) { /* If the String is null... */ } else if (str.equals ("Some Possibility")) { /* ... */ } else if (str.equals ("Another Possibility")) { /* ... */ } else if (str.equals ("Possibility # 3")) { /* ... */ } // Etc, etc, etc else { /* DEFAULT Operation */ } }
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?
How many people have code in their Sigs?
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Java Forum
- Previous Thread: Help
- Next Thread: Logical operations and data-checks



Linear Mode