3

Note: This is not a duplicate of Java switch statement: Constant expression required, but it IS constant because the solution to that link was already applied here.


In a Cordova App with typescript I use this enum to send my actions =

typescript

enum PluginActions {
   PICK = "PICK",
   PICK_MULTIPLE = "PICK_MULTIPLE"
}

I send that to cordova and in Java I get that as an action string variable in my method.

@Override
  public boolean execute(String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException {

  }

There I also have an enum.

Java

enum PickerActions {
  PICK, PICK_MULTIPLE
}

I want to compare the typescript PluginActions vs the java PickerActions.

Using if I can do it using:

if (action.equals(PickerActions.PICK.name())) { }

but I wan to do it with a switch so I can easily add more actions

  switch (action) {
    case PickerActions.PICK.name():
      JSONObject filters = inputs.optJSONObject(0);
      this.chooseFile(filters, callbackContext);
      return true;
    default:
    Log.w(this.LOGGER_TAG, "No function was found for " + action);
    return false;      
  }

But I get an error there: error: constant string expression required

Is there a way to use an enum string name in a switch statement?

Edit:

As per @Arnaud recommendation I casted the value of the enum in this way:

final PickerActions pickerAction = FilePickerActions.valueOf(action);

    switch (pickerAction ) {
      case PickerActions.PICK:
        JSONObject filters = inputs.optJSONObject(0);
        this.chooseFile(filters, callbackContext);
        return true;
      default:
      Log.w(this.LOGGER_TAG, "No function was found for " + action);
      return false;      
    }

But I get another error there regarding case PickerAction.Pick

error: an enum switch case label must be the unqualified name of an enumeration constant

distante
  • 6,438
  • 6
  • 48
  • 90
  • 1
    Possible duplicate of [Java switch statement: Constant expression required, but it IS constant](https://stackoverflow.com/questions/3827393/java-switch-statement-constant-expression-required-but-it-is-constant) – Gaurav Jeswani Sep 27 '19 at 08:25
  • 1
    Switch on `PickerActions.valueOf(action)`, and put actual enum values in the `case` blocks : `case PickerActions.PICK` . – Arnaud Sep 27 '19 at 08:27
  • @GauravJeswani that link shows a similar problem but the solution is to use Enums, what I am already doing. – distante Sep 27 '19 at 08:31
  • @Arnaud I try it but I get another error. `error: an enum switch case label must be the unqualified name of an enumeration constant` (I have updated my question with that info) – distante Sep 27 '19 at 08:34

2 Answers2

5

You can use java.lang.String values in your java.lang.Enum and test against it in your switch cases.

But, like @SpencerSprowls said in comments, it will throw an java.lang.IllegalArgumentException if you use a value that doesn't match with any values of your java.lang.Enum.

Thus, adding a default case in your switch is useless. To avoid throwing unwanted exceptions, you can convert your java.lang.Enum values to an java.util.EnumMap and check if it matches existing values before the switch with EnumMap#containsValue

private enum PickerActions {
    PICK("PICK"),
    PICK_MULTIPLE("PICK_MULTIPLE");
    private final String value;
    PickerActions(final String value) {
        this.value = value;
    }
    @Override
    public String toString() {
        return value;
    }
}
    
private static boolean test(String test) {
     switch (PickerActions.valueOf(test)) {
        case PICK:
            //
            return true;
        case PICK_MULTIPLE:
            //
            return false;
        // default:
            // This will never happen
            // return false;      
    }
}

Here is a working example

Paul
  • 1,410
  • 1
  • 14
  • 30
  • 1
    This will throw an exception if the String does not correspond to a valid Enum. There is no use case where the default will ever be hit. –  Mar 28 '21 at 19:52
2

error: an enum switch case label must be the unqualified name of an enumeration constant, You should not use case PickerActions.PICK but to only case PICK as below,

public class MainClass {
   enum Choice { Choice1, Choice2, Choice3 }
   public static void main(String[] args) {
      Choice ch = Choice.Choice1;

      switch(ch) {
         case Choice1:
            System.out.println("Choice1 selected");
            break;
         case Choice2:
            System.out.println("Choice2 selected");
            break;
         case Choice3:
            System.out.println("Choice3 selected");
             break;
      }
   }
}
madhepurian
  • 271
  • 1
  • 13