SkyeBoniwell
SkyeBoniwell

Reputation: 7122

how to use Switch with a string

I have the code below that I wrote to get rid of a huge mess of if then else statements. The only problem is, I just found out that the Switch statement cannot be used with a string.

Is there a trick to get around this?

Thanks

switch(xpp.getName()) {
            case("creature") : attribID = Integer.parseInt(xpp.getAttributeValue(0));
            case("name") : elName = xpp.getName(); break;
            case("race") : elName = xpp.getName(); break;
            case("gender") : elName = xpp.getName(); break;
            case("alignment") : elName = xpp.getName(); break;
            case("age") : elName = xpp.getName(); break;
            case("condition") : elName = xpp.getName(); break;
            case("skinColor") : elName = xpp.getName(); break;
            case("hairColor") : elName = xpp.getName(); break;
            case("size") : elName = xpp.getName(); break;
            case("height") : elName = xpp.getName(); break;
            case("weight") : elName = xpp.getName(); break;
            case("hitPoints") : elName = xpp.getName(); break;
            case("armorClass") : elName = xpp.getName(); break;
            case("actionPoints") : elName = xpp.getName(); break;
            case("magicPoints") : elName = xpp.getName(); break;
            case("strength") : elName = xpp.getName(); break;
            case("dexterity") : elName = xpp.getName(); break;
            case("intelligence") : elName = xpp.getName(); break;
            case("weapon") : elName = xpp.getName(); break;
            case("armor") : elName = xpp.getName(); break;
            case("magicItem") : elName = xpp.getName(); break;
            case("attackSpell") : elName = xpp.getName(); break;
            case("defenseSpell") : elName = xpp.getName(); break;
            case("item1") : elName = xpp.getName(); break;
            case("item2") : elName = xpp.getName(); break;
        }

Upvotes: 1

Views: 622

Answers (5)

smessing
smessing

Reputation: 4350

As an alternative, if you can't just switch to Java 7, the simplest method would be to replace the switch statement with a bunch of if else's. Alternatively, you could try switching on the hashCode of the string, but I'd only recommend that if you're sure you have no collisions (hashCode() is not guaranteed to prevent collisions!). The clearest and most simple solution would be using if else's. It maintains readability and allows you to still support older versions of Java.

EDIT: Only one case does something different! Why not simply do:

if (xpp.getName().equals("creature") {
    attribID = Integer.parseInt(xpp.getAttributeValue(0));
} else {
    elname = xpp.getName();
}

Upvotes: 3

nist
nist

Reputation: 1721

You are doing the same thing for all cases exept then xpp.getName() is "creature". This means that all your code can easily be transformed into a singel if statement

if ( xpp.getName().equal("creature"))
     attribID = Integer.parseInt(xpp.getAttributeValue(0));
else
     elName = xpp.getName();

It also looks like you should start to program more object orientated and create some classes instead of using a name to determine what the object is

Upvotes: 2

Andreas Dolk
Andreas Dolk

Reputation: 114807

Java 7 supports switch on Strings, but it won't work with android.

I'd recommend using enums. Create one enum class and name the enums like your expected strings. Then you can determine the enum for a given String and switch on it.

Here's an example:

Number.java

public enum Number { one, two, three }

Switcher.java

public class Switcher {
    public static void main(String[] args) {
        String input = "one";
        switch (Number.valueOf(input)) {   // <- that's the magic!
        case one:System.out.println("1");break;
        case two:System.out.println("2");break;
        case three:System.out.println("3");break;
        }
    }
}

Upvotes: 2

theglauber
theglauber

Reputation: 29645

Re-think your design. Make it more object-oriented. Use anonymous classes, maybe, or some kind of dispatch table. Or just simplify it. It looks like there are only 2 choices anyway ("creature" does one thing, eveything else does the other). Code that is hard to maintain is a prime candidate for refactoring.

Upvotes: 2

Emil L
Emil L

Reputation: 21111

Switches for strings is only available for Java 7. So you need to use a Java 7 SDK for compiling your code.

However, if you are using Eclipse as an IDE for developing an Android application you won't be able to set the code compliance level to Java 7 for your android project. You will then either have to wait for the android folks to support java 7 or alternatively use IntelliJ or compile your code from command line with the java 7 sdk and then use the dex compiler...

Upvotes: 1

Related Questions