4

Possible Duplicate:
Switch Statement with Strings in Java

Im using the following code and I wonder if there is a way to do it with switch , the reason that I don't use it as default since type name is type string.(I know that this option is supported in 1.7 version but I need to use 1.6) There is a way to overcome this problem ?

public static SwitchInputType<?> switchInput(String typeName) {

        if (typeName.equals("Binary")) {
            return new SwitchInputType<Byte>(new Byte("23ABFF"));
        }
        else if (typeName.equals("Decimal")) {
            return new SwitchInputType<BigDecimal>(new BigDecimal("A"));
        }
        else if (typeName.equals("Boolean")) {
            return new SwitchInputType<Boolean>(new Boolean("true"));
3
  • 5
    Yes, you can do this with value off as explained here stackoverflow.com/questions/338206/… Commented Jan 24, 2013 at 8:18
  • stackoverflow.com/a/338230/637889 has a Before JDK 7 section with an example using the enum type Commented Jan 24, 2013 at 8:21
  • Actually this answer is the duplicate Commented Jan 24, 2013 at 8:31

2 Answers 2

4

As explained in other answers, you can't use switch statement with strings if you're working with Java 1.6.

The best thing to do is to use an enumerator instead of string values:

public static SwitchInputType<?> switchInput(InputType type) {
    switch(type){
        BINARY:
            return new SwitchInputType<Byte>(new Byte("23ABFF"));
        DECIMAL:
            return new SwitchInputType<BigDecimal>(new BigDecimal("A"));
        BOOLEAN:
            return new SwitchInputType<Boolean>(new Boolean("true"));
    }
}

where:

public enum InputType{
    BINARY, DECIMAL, BOOLEAN // etc.
}

UPDATE:

In your Field class add an InputType fieldType property. Then inside the loop:

MemberTypeRouting.switchInput(field.getFieldType());
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks but here you write BINARY, DECIMAL, BOOLEAN in upper case and I should get the like Boolean Binary and etc ... In addition I get String like Edm.Int16 Edm.Boolean etc so how should I handle it?
If you can't change your code replacing returned string values with enum values, you can't use switch statement...
If I want to do it how should I try?
can you post the code of the metod is returning these typeName strings?
for (Field field : declaredFields) { String memberName = field.getName(); witchInputType<?> switchInput = MemberTypeRouting.switchInput(fieldTypeName
|
2

Switches with Strings are only supported since Java 7. Sadly it was not supported in older versions, so you cannot use it wit Java 6, and you'll have to stay with the if/else statements you are already using.

See also this question, asked a few years back: Why can't I switch on a String?

1 Comment

what does it mean that I need to create class (ENUN)with all the expected values and than ask for them?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.