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.
niamul 0 Newbie Poster
Recommended Answers
Jump to PostYou don't need the switch. And you cover only String with letters 'a' to 'z'. For a better approach:
…String string2 = ""; char char1; for (int x = 0; x < input.length; x++){ char1 = input.charAt(x); int i = char1; string2 += i; } return string2;
Jump to PostSorry if you felt the post was negative. If you're here to learn something, I hope you learned something from it, such as a little taste of how a switch works and why this sort of thing simply can't work, considering what a switch actually is. To the compiler, it's …
Jump to PostSorry, I thought I'd made that clear (about the function calls as cases) in the previous post. I guess I took it out, though. Anyway, what the switch generates looks more like a lookup table, where the if/else if/else if method generates a series of test-and-branch instructions. The lookup table …
Jump to PostSwitch can accept int or char. Everything else would be better suited with an appropriate logic test inside of an if-statement.
...well it can take Strings too, if you parse them (switch(Integer.parseInt(string))) {}
But why bump a thread that is 3 months old?
Jump to PostActually, the new OS X does not come with Java installed, and Apple is, as far as I know, pushing Java development and maintenance off to the OpenJDK. Java on the mac is not a sure thing, not that it ever was very solid.
Honestly, I think Java 7 …
All 43 Replies
jwenting 1,905 duckman Team Colleague
paradox814 1 Posting Whiz
niamul 0 Newbie Poster
jwenting 1,905 duckman Team Colleague
cyberowl 0 Newbie Poster
jwenting 1,905 duckman Team Colleague
cyberowl 0 Newbie Poster
Fhernd commented: It is clear. +0
jwenting 1,905 duckman Team Colleague
cyberowl 0 Newbie Poster
jwenting 1,905 duckman Team Colleague
rLh 0 Newbie Poster
Ficke 0 Newbie Poster
staneja 0 Junior Poster in Training
manojdesai 0 Newbie Poster
Cudmore 9 Junior Poster in Training
jwenting 1,905 duckman Team Colleague
nnprasad2000 0 Newbie Poster
Boyett Martinet 0 Newbie Poster
Boyett Martinet 0 Newbie Poster
BestJewSinceJC 700 Posting Maven
rameshrokz 0 Newbie Poster
pranalib 0 Newbie Poster
sadaf Nasim 0 Newbie Poster
Tsorf_Dev 0 Newbie Poster
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
Tsorf_Dev 0 Newbie Poster
jon.kiparsky commented: Don't post broken code! +0
jon.kiparsky 326 Posting Virtuoso
jon.kiparsky 326 Posting Virtuoso
Tsorf_Dev 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.