The Artima Developer Community
Sponsored Link

Java Answers Forum
using enumerations and Switch

4 replies on 1 page. Most recent reply: Mar 27, 2006 4:26 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

using enumerations and Switch Posted: Mar 24, 2006 10:02 AM
Reply to this message Reply
Advertisement
I would like to know if there is a way to do the following:

Here is a simple Switch, but I am basically hardcoding the switch argument.

enum Color{red,green,blue}
 
class SwitchEnum{
 
            public static void main(String[] args){
 
                        Color c = Color.green;
 
                        switch(c){
                                    case red: System.out.println(“red”);
                                    break;
 
                                    case green: System.out.println(“green”);
                                    break;
 
                                    case blue: System.out.println(“blue”);
                                    break;
                                    default: System.out.println(“not found”); 
                        }
            }
}
 


I want to be able to assign it based on user input. If I am restricted to assigning the Color value, then I will need to have an if/else contruct to decide which enum variable to populate like such:
if (cmd.equals("green")) 
     Color c = Color.green;
 
     Else if(cmd.equals(“red”))
              Color c = Color.red;

Etc…

now process the switch statement.

I would MUCH rather have the ability to do something like:

String input = “green”;
Color c = (Color)input;

now process the switch statement

is there anyway to do this without having to have the if else to do the decision making process and I can do it on the fly with cleaner code?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: using enumerations and Switch Posted: Mar 27, 2006 12:24 AM
Reply to this message Reply
Doesn't work that way.

1. I don't think you can use a switch on enum. Use
    if (c == Color.green){
    } else if (c == Color.red) {
    } else {
    }

instead.

2. You can't convert a String to a enum value.
Do this instead:
    private static Color getColor(String inputString) {
        Color inputColor = null;
        for (Color col : Color.values()) {
            if (col.toString().equalsIgnoreCase(inputString)) {
                inputColor = col;
        }
        return inputColor;
    }


Note: I used differnt names on purpose, so it is easier to understand.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: using enumerations and Switch Posted: Mar 27, 2006 1:21 AM
Reply to this message Reply
Update:

Instead of the method mentioned above, use simply:
    Color.valueOf(inputString);

Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

Re: using enumerations and Switch Posted: Mar 27, 2006 4:19 AM
Reply to this message Reply
Wonderful! can't wait to give that a try, thank you for your time. btw, you can use enums in switch statements, the code I provided does in fact work.

take care

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: using enumerations and Switch Posted: Mar 27, 2006 4:26 AM
Reply to this message Reply
Thanks.

Seems I misinterpreted the compiler error.

I always tried:
    switch (c) {
        case Color.red:
            break;
    }


which doesn't work.

Flat View: This topic has 4 replies on 1 page
Topic: Best practices for evolving existing J2EE code Previous Topic   Next Topic Topic: Eclipse RCP Help!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use