简体   繁体   English

enum.ValueOf是否需要标准路径?

[英]enum.ValueOf requires fully qualified path?

I am trying to use valueOf in Java to parse a String into an enum and it seems to be requiring a fully qualified path to the enum as the String which controverts examples on SO and other places. 我正在尝试使用Java中的valueOf将String解析为枚举,并且似乎要求使用枚举作为String的枚举的完全限定路径,这会混淆SO和其他地方的示例。 For example: 例如:

String sRange = "region";
order.range = ItemOrder.eRange.valueOf( sRange );

where ItemOrder.eRange is: 其中ItemOrder.eRange为:

class ItemOrder {
    public enum eRange {
        area,
        zone,
        region
    }
}

gives the error: 给出错误:

Exception in thread "main" java.lang.IllegalArgumentException: No enum constant ItemOrder.eRange.region" at java.lang.Enum.valueOf(Enum.java:236) 线程“主”中的异常java.lang.IllegalArgumentException:java.lang.Enum.valueOf(Enum.java:236)上没有枚举常量ItemOrder.eRange.region

The problem is somewhat subtle: a quotation mark. 这个问题有些微妙:引号。

If you look closely at the error message, you will see that the String being supplied to valueOf is not region , but is region" with a trailing quotation mark. It looks like the quotation mark is part of the error message, but it is not, it is part of the value, and that is leading to the mismatch. 如果仔细查看错误消息,您将看到提供给valueOf的字符串不是region ,而是带有结尾引号的region" 。看起来引号是错误消息的一部分,但不是,它是值的一部分,并导致不匹配。

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

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