1

In my application, I receive a response that has hard-coded values it can be. I have created an enum with those responses as so:

enum ResponseCode {
    APPROVED("AP"),
    REJECTED("RJ"),
    ...;

    private final String code;

    private ResponseCode(String code) {
        this.code = code;
    }
}

I would handle a response differently depending on which code I receive. I thought of using a switch-case to solve this problem.

String response = functionThatReturnsString(); // Could be "AP" or "RJ" etc.

switch(response) {
case ResponseCode.APPROVED:
    // Response handling here
    break;
...
}

However, I get the error Type mismatch: cannot convert from ResponseCode to String

Is there a way to get around this? Or is there a better way to do this?

  • 3
    I suggest you write a method in `ResponseCode` to convert a string code to the corresponding `ResponseCode`. – Jon Skeet Aug 10 '23 at 07:01
  • 1
    `ResponseCode rc= ResponseCode.valueOf(response);`, then switch on that value. – Andy Turner Aug 10 '23 at 07:09
  • Please share an [mcve] – Jens Aug 10 '23 at 07:14
  • @AndyTurner I m confused as to why it is not a constant value. Isnt it already set inside the enum that the value of the response code is a certain final value APPROVED --> "AP"? Why is it not valid? – experiment unit 1998X Aug 10 '23 at 07:24
  • 2
    @experimentunit1998X the Java Language Specification has a narrow definition of constant expressions: things like literals are, things like method invocations are not. The compiler has no way of knowing that the method body only returns a constant-valued field. – Andy Turner Aug 10 '23 at 07:26

3 Answers3

3

You are switching on a String, but you are using ResponseCode as the cases, which doesn't make sense. A String can never match a ResponseCode.

There are multiple ways around this. Instead of an enum, you can use static final constants:

final class ResponseCode {
    private ResponseCode() {}
    public static final String APPROVED = "AP";
    public static final String REJECTED = "RJ";
}
switch (someResponseCode) {
    case ResponseCode.APPROVED -> { ... }
    case ResponseCode.REJECTED -> { ... }
    // remember to handle the case of not matching any response code!
    default -> { ... }
}

Alternatively, write a method that converts Strings to your enum:

public static ResponseCode fromString(String s) {
    return switch (s) {
        case "AP" -> APPROVED;
        case "RJ" -> REJECTED;
        default -> null;
    };
    // or: Arrays.stream(ResponseCode.values()).filter(x -> x.code.equals(s)).findFirst().orElse(null);
}

Note that you can remove the code field, if you don't need it for anything else.

Then you can switch like this:

switch (ResponseCode.fromString(someResponseCode)) {
    case APPROVED -> { ... }
    case REJECTED -> { ... }
    // remember to handle the case of not matching any response code!
    case null -> { ... }
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Null isn't a supported case value, is it? (at least not for Java 11: https://stackoverflow.com/questions/10332132/how-to-use-null-in-switch) – knittl Aug 10 '23 at 11:46
  • @knittl Oh really? I was using Java 17 and it worked so I didn't think too much about it. – Sweeper Aug 10 '23 at 11:48
  • Oh, I see, those are switch expressions, not statements. Nevermind then – knittl Aug 10 '23 at 11:49
2

Convert your String to a ResponseCode:

ResponseCode rc= ResponseCode.valueOf(response);

then switch on that value:

switch (rc) {
  case APPROVED:
    // Response handling here
    break;
...
}

You should probably also handle the case where the String can't be converted to an enum value.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

You can use the enum directly:

ResponseCode responseCode = ...

switch(responseCode) {
case APPROVED:
    // Response handling here
    break;
...
}
Jens
  • 67,715
  • 15
  • 98
  • 113
  • 1
    I don't think that's the problem. OP is trying to switch on the String value using enum constants as cases. – Jorn Aug 10 '23 at 07:03
  • @AndyTurner sorry don't get you. I do not qualify the enum – Jens Aug 10 '23 at 07:09
  • @AndyTurner OP is use the switch on a string I use it on the enum. But anyway it is unclear where the string comes from – Jens Aug 10 '23 at 07:13