Madinat al-Muslimeen Islamic Message Board
Java/switch command with strings |
---|
bhaloo |
07/26/01 at 11:36:17 |
slm Did you know the switch command doesn't work with strings? Any ideas how to get around this? i.e. string = "kashif" switch (string) { case "bhaloo": system.out.println("Hey dude") ; case "kashif": system.out.println("Jolly good") ; case "bro.haniff": system.out.println("Tiddlywinks anyone?") ; default: break; } |
Re: Java/switch command with strings |
---|
meraj |
07/26/01 at 15:08:43 |
slm, hmm.. i dunno anythign about java, but can you liek assign a string to a variable instead and tell it to print the string variable instead of just the string itself ??? ? |
Re: Java/switch command with strings |
---|
bhaloo |
07/26/01 at 16:51:35 |
slm Actually the problem is the switch statement. Java won't let you do a switch statement on a string type, but it will on a char, int, boolean. This is where I have the problem, I want the code to do waht I did up above, but it wont compile because its a string instead of the other types. So the question is how do you do switches with strings. One solution is to use if-(else) statements, but that is too lengthy. |
Re: Java/switch command with strings |
---|
meraj |
07/26/01 at 17:09:33 |
slm, hmm.. ok if i understand correctly what you're sayin, does java let you define a custom data type? like 'enum' in c++ for instance? or am i still missing the question here ??? causeiknwo what you're sayin, it doesnt allow specifically the string data type to be used in the switch case, but is there some way you can use the string variable as a string data type but make the case accpet it in some way? or is that the exact thing you just asked? :( ok sorry i have no idea ;) |
Re: Java/switch command with strings |
---|
Kashif |
07/26/01 at 17:40:50 |
assalaamu alaikum bhaloo, you can't use Strings in switch blocks, full-stop, and as far as i know there isn't a work around for it. The if-else route seems to be the way to go. Kashif Wa Salaam |
NS |
Re: Java/switch command with strings |
---|
amal |
07/26/01 at 20:55:57 |
Assalamu alaykum, The if-then-else is propably the easiest way to accomplish the task but if you still want to use the switch statement you can try to simulate an enumeration with an array of strings and then use the index for your switch statement. String[] mystrs={str1,str2,...,strN}; for (int index=0;index<n;index++) { if (stringUnderComparison.equals(mystrs[index])) { switch (index) { case 1:.....;break; case 2:.....;break; . . . case n:....;break; default:...;break; } break; } } |
bhaloo |
07/26/01 at 23:37:07 |
slm Amal, that is an interesting approach but the execution time is twice that of the if-else string situation. First you have the for loop to go through every iteration and then the switch/case statement will go through every case statement till it hits the correct result. The if-else just goes through the whole thing once maximum (with a break included). |
Re: Java/switch command with strings |
---|
BroHanif |
07/27/01 at 06:07:20 |
bhaloo How about using PHP instead of Java would that work ??? Salaams Hanif |
Re: Java/switch command with strings |
---|
meraj |
07/27/01 at 06:11:43 |
slm, hmm.. yeah php is a lot easier to use than java, but theyre completely different.. java is a programming language and php is a scripting language.. they have completely different compilers/interpreters. and i think to do what hes trying to do, you need java which is somewhat more powerful for creating some applications :) |
Re: Java/switch command with strings |
---|
zamzam |
07/27/01 at 08:22:03 |
[center][color=Green][slm] Well, you still work w/ number but is more readable. Hope it helps. [/center] static final BHALOO = 1; static final KASHIF = 2; static final BRO_HANIF = 3; switch (someIntVar) { case BHALOO : system.out.println("Hey dude") ; case KASHIF : system.out.println("Jolly good") ; case BRO_HANIF : system.out.println("Tiddlywinks anyone?") ; default: break; } [wlm] [/color] [/center] Zamzam |
Re: Java/switch command with strings |
---|
amal |
07/27/01 at 08:26:28 |
Assalamu alaykum, [quote]the execution time is twice that of the if-else string situation[/quote] Actually the way i wrote it will yield a Big O of N^2 :) That's what happens when you haven't coded in a year :( Now, i thought the big O for an if-then-else statement was N(where N is the number of comparisons) and that the switch statement was 1 in which case the above algorithm would yield a Big O of N+1...Are you saying that the if-then-else statement yields 1 and the switch N??? |
Re: Java/switch command with strings |
---|
Anonymous |
07/27/01 at 23:50:58 |
assalaam-o-'alaikum You can always use a map/dictionary/hashtable. I don't know much java, but I do know that collection classes in java support all of these. You can associate each name with a fixed integer constant. Before the switch, you get the integer constant associated with the name through one of those gazillion java apis. The indexing time is constant like an array. You'll end up with a corpulent piece of code though. This solution is appropriate if you have a large # of names but is ridiculous for , like, 3 entries. |
Individual posts do not necessarily reflect the views of Jannah.org, Islam, or all Muslims. All trademarks and copyrights on this page are owned by their respective owners. Comments are owned by the poster and may not be used without consent of the author.The rest © Jannah.Org |