java 在java中找不到类型的枚举常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42344930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:33:26  来源:igfitidea点击:

No Enum constant found for type in java

javaenums

提问by glen maxwell

I am using enumin java, Here is the enum

enum在java中使用,这是枚举

public enum AbuseSectionType{

    MUSIC("Music"), DANCE("Dance"), SOLO("Solo"), ACT("Act")

    private String displayString;

    AbuseSectionType(String displayValue) {
        this.displayString = displayValue;
    }

    @JsonValue
    public String getDisplayString() {
        return displayString;
    }

    public void setDisplayString(String displayString) {
        this.displayString = displayString;
    }
}

I am trying to get value AbuseSectionType.valueOf("Music"). I am getting no enum constant and found no error. I am supposed to have value MUSIC.

我正在努力获取价值AbuseSectionType.valueOf("Music")。我没有得到枚举常量,也没有发现错误。我应该有价值MUSIC

回答by Winter

The name()of an enum is the name specified when declaring it, MUSICin your case.

name()枚举的是它声明时,指定的名称MUSIC在您的案件。

If we read the javadoc for valueOf():

如果我们阅读 javadoc valueOf()

Returns the enum constant of the specified enum type with the specified name.

返回具有指定名称的指定枚举类型的枚举常量。

valueOf()is using the name()of the enum. But what you want to achieve is different, so you cannot use this method. What you can do instead is to make your own method that finds the value from your own field (displayString).

valueOf()正在使用name()枚举的 。但是你要达到的目的不一样,所以不能用这个方法。相反,您可以做的是创建自己的方法,从您自己的字段 ( displayString)中找到值。

Here's an example:

下面是一个例子:

public static AbuseSectionType fromDisplayString(String displayString)
{
    for(AbuseSectionType type : AbuseSectionType.values())
        if(type.getDisplayString().equals(displayString)
            return type;

    return null; //not found
}

回答by Raven

The default valuOf()method will only retrieve the respective enmumif the exact spelling of the enum-definition is used. In your case you have defined the enum MUSICso in order to get that one you have to do it like this: AbuseSectionType.valueOf("MUSIC");

如果使用枚举定义的确切拼写,则默认valuOf()方法将仅检索各自enmum的。在您的情况下,您已经定义了枚举,MUSIC因此为了获得该枚举,您必须这样做:AbuseSectionType.valueOf("MUSIC");

In order to achieve what you seem to want you have to implement a method in the enum class by yourself. For your example you could do somthing like this:

为了实现您似乎想要的东西,您必须自己在枚举类中实现一个方法。对于您的示例,您可以执行以下操作:

public AbuseSectionType resolve(String name) {
    for(AbuseSectionType current : AbuseSectionType.values()) {
        if(current.displayString.equals(name)) {
            return current;
        }
    }

    return null;
}

回答by Ashutosh Jha

use AbuseSectionType.valueOf("MUSIC") pass the name of enum. See java docs regarding use of valueOf

使用 AbuseSectionType.valueOf("MUSIC") 传递枚举的名称。有关 valueOf 的使用,请参阅 java 文档