Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,301 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 779 people online right now. Registration is fast and FREE... Join Now!




string in switch case

 
Reply to this topicStart new topic

string in switch case, How to switch-case on string

kornni
4 Jan, 2009 - 07:00 AM
Post #1

New D.I.C Head
*

Joined: 1 Jul, 2008
Posts: 10


My Contributions
Hi All

I want to switch statement on string, But I don't know

I know only use if-else but my teacher not use if-else he want switch-case

CODE
  switch(Name){
                case "S0":
                    if(input == '0' )
                        Name = "S1";  //input 0 next state = S1
                    else if (input == '1')
                        Name = "S0";  //input 1 next state = S0
                    break;
                case "S1":
                    if(input == '0')
                        Name = "S1";
                    else
                        Name = "S2";
                    break;
            default:
                    break;
        }
        return Name;              
    }


User is offlineProfile CardPM
+Quote Post


ayman_mastermind
RE: String In Switch Case
4 Jan, 2009 - 07:14 AM
Post #2

I live to code and code to live...
Group Icon

Joined: 12 Dec, 2008
Posts: 1,645



Thanked: 77 times
Dream Kudos: 425
My Contributions
actually you cant use switch statements on strings, you could use them either on numbers or on chars. so one solution is to convert your string into char and use single quotes ' ' or you will have to create a map with the strings as keys and integer values indexing an array of integers as values that you can use as indices for the switch statement. Hope that helps, good luck in your programming wink2.gif
User is offlineProfile CardPM
+Quote Post

baavgai
RE: String In Switch Case
4 Jan, 2009 - 08:10 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 3,572



Thanked: 268 times
Dream Kudos: 525
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Sorry, switch doesn't work with string.

Don't worry, switch is almost always a poor choice, anyway. It lacks flexibility and, as you've seen, has some major limitations. Here's the logic you offered in a standard if-then:

java

if(Name.equals("S0") && input=='0') {
Name = "S1";
} else if(Name.equals("S1") && input!='0') {
Name = "S2";
}


edit: oops'd my if


This post has been edited by baavgai: 4 Jan, 2009 - 08:25 AM
User is offlineProfile CardPM
+Quote Post

macosxnerd101
RE: String In Switch Case
4 Jan, 2009 - 02:33 PM
Post #4

D.I.C Regular
Group Icon

Joined: 27 Dec, 2008
Posts: 389



Thanked: 15 times
My Contributions
The only datatypes you can use a switch on are: char, byte, short and int. Simply use a series of if statements in its place for your problem. You can, for other problems, do something like:
CODE

String x = "123";
for(int i = 0; i < x.length(); i++){
  switch(x.charAt(i)){
         //Code Here
         }
   }

User is online!Profile CardPM
+Quote Post

Locke
RE: String In Switch Case
4 Jan, 2009 - 03:42 PM
Post #5

I am *not* a Thief...I *prefer* TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 2,809



Thanked: 185 times
Dream Kudos: 325
Expert In: Java

My Contributions
QUOTE(kornni @ 4 Jan, 2009 - 07:00 AM) *
I know only use if-else but my teacher not use if-else he want switch-case


Then...honestly, he's a bit of an idiot if he told you to use switch-case on Strings themselves.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 05:24PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month