Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch with string in Java1.6 [duplicate]

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"));
like image 388
Stefan Strooves Avatar asked Sep 18 '25 18:09

Stefan Strooves


1 Answers

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());
like image 115
davioooh Avatar answered Sep 21 '25 07:09

davioooh