简体   繁体   English

如何在Java的switch语句中将字符串与枚举进行比较?

[英]How to compare string to enum in a switch statement in Java?

I guess I did not yet get the concept of Enum s in Java. 我想我还没有在Java中得到Enum的概念。

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? 我究竟做错了什么?

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 . 不幸的是,您将不得不以不同的方式处理null情况,因为将null放入switch中将引发NullPointerException

EnumTestOperator.NOT_EQUAL is an instance of EnumTestOperator . EnumTestOperator.NOT_EQUAL是实例EnumTestOperator You cannot compare it with a String . 您无法将其与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). 您甚至可以将EnumTestOperator.NOT_EQUAL.toString()String进行比较,但不能在case语句中进行比较( case语句需要Java中的常量表达式)。

But you can instead iterate on EnumTestOperator constants : 但是您可以改为对EnumTestOperator常量进行迭代:

public static EnumTestOperator find(String strValue) {
  for(EnumTestOperator operator : EnumTestOperator.values()) {
    if(operator.toString().equals(strValue)) {
      return operator;
    }
  }
  throw new IllegalArgumentException("No matching value");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM