2

I guess I did not yet get the concept of Enums in Java.

I try to compare Strings to my Enum-entries comparable to Special characters in an enum.

package com.stackoverflow.tests;

public class EnumTest {

  enum EnumTestOperator {
    EQUAL("=="),
    NOT_EQUAL("!=");

    private String value;

    private EnumTestOperator(String value) {
      this.value = value;
    }

    public String toString() {
      // will return == or != instead of EQUAL or NOT_EQUAL
      return this.value;
    }

  }

  public static void main(String[] args) {
    String line;
    line  = "<>";
    line  = ".ne.";
    line  = "!=";

    // Operator
    switch(line) {
//      case "!=":
      case EnumTestOperator.NOT_EQUAL:
        System.out.println("Not Equal");
        break;
      default:
        System.out.println("Something else");
        break;
    }
  }

}

But in the Line:

case EnumTestOperator.NOT_EQUAL:

I get a compiler error:

Type mismatch: cannot convert from EnumTest.EnumTestOperator to String

What am I doing wrong?

Community
  • 1
  • 1
Edward
  • 4,453
  • 8
  • 44
  • 82

2 Answers2

5

You are comparing incompatible types. It's similar to the statement

"some string" == EnumTestOperator.NOT_EQUAL

where your compare a string value to an enum constant.

Just stop using enums and declare them as string constants:

private static final String EQUAL = "==";
private static final String NOT_EQUAL = "!=";

Then you can use them in the switch/case.


The other solution would be to find the enum constant based on your input. Add this to your enum:

public static EnumTestOperator byValue(String val){
  for(EnumTestOperator en:values()){
    if(en.value.equals(val)){
      return en;
    }
  }
  return null;
}

And then use the function like this:

EnumTestOperator en = EnumTestOperator.byValue(line);
switch(en) {
  case NOT_EQUAL:
    System.out.println("Not Equal");
    break;
  default:
    System.out.println("Something else");
    break;
}

Unfortunately you will have to handle the null case differently since putting a null into a switch will throw a NullPointerException.

f1sh
  • 11,489
  • 3
  • 25
  • 51
  • I think you could also use the predefined method 'valueOf()'. See [here](https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#valueOf(java.lang.Class,%20java.lang.String)) – zypro Mar 30 '16 at 11:51
  • @zypro no, you cannot. ``valueOf`` expects the constant's name (such as ``"NOT_EQUAL"``, which is not given here. – f1sh Mar 30 '16 at 12:07
1

EnumTestOperator.NOT_EQUAL is an instance of EnumTestOperator. You cannot compare it with a String.

You can evenutually compare EnumTestOperator.NOT_EQUAL.toString() with a String but not in a case statement (case statements require constant expressions in Java).

But you can instead iterate on EnumTestOperator constants :

public static EnumTestOperator find(String strValue) {
  for(EnumTestOperator operator : EnumTestOperator.values()) {
    if(operator.toString().equals(strValue)) {
      return operator;
    }
  }
  throw new IllegalArgumentException("No matching value");
}
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148