简体   繁体   中英

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. For example:

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

where ItemOrder.eRange is:

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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