Java enum elements with spaces?
我正在使用Java,我创建了一个枚举,如下所示:
1 2 3 4 5 6 7 | public enum myEnum { india, russian, england, north America } |
上面的示例在以元素名称(即北美)使用空格时给出了错误。
有什么建议如何解决上述问题?
您不能在标识符中间放置空格。
这样做结束了标识符,并且解析器假定接下来要执行的操作是该语句上下文中的有效令牌。很少(如果有)合法的地方。
常规的Java值名称为:
1 2 3 | INDIA, // Or India, RUSSIA, // Russia, NORTH_AMERICA; // NorthAmerica; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public enum CountryAndOneContinent { INDIA("India"), RUSSIA("Russia"), NORTH_AMERICA("North America"); private String displayName; CountryAndOneContinent(String displayName) { this.displayName = displayName; } public String displayName() { return displayName; } // Optionally and/or additionally, toString. @Override public String toString() { return displayName; } } |
我对使用
我更喜欢方法明确地传达其目的,因为它更具表现力和明显性。
我很想继续,为什么要在名称中留一个空格?因为您想将其引用为字符串。
这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
您可以考虑覆盖字符串。
1 2 3 | public toString() { return name; } |
覆盖toString()的一个优点是,该字符串也可以在MyEnum .valueOf(myString)中使用。因此,重写toString基本上会创建一个枚举值的HashMap。
这个问题(特别是)与枚举无关:在Java中,名称不能有空格。尝试消除空格(使用大写字母区分),或者改用下划线。
将它们像
Java命名规则不允许在变量,类,枚举和枚举成员(以及所有其他标识符)的名称中使用空格作为可能的字符。因此,这个"问题"无法解决。只需使用" north_america"作为成员名称即可!
An identifier is an unlimited-length sequence of Java letters and Java
digits, the first of which must be a Java letter.The Java letters include uppercase and lowercase ASCII Latin letters
A-Z (\\u0041-\\u005a), and a-z (\\u0061-\\u007a), and, for historical
reasons, the ASCII underscore (_, or \\u005f) and dollar sign ($, or
\\u0024). The $ character should be used only in mechanically generated
source code or, rarely, to access preexisting names on legacy systems.The"Java digits" include the ASCII digits 0-9 (\\u0030-\\u0039).
只需在枚举类中创建自己的valueOf like函数即可。用下划线替换空格。将您的枚举常量命名为" north_america(" North America")"。如果该方法找不到您的枚举,则仅返回该参数。
1 2 3 4 5 6 7 8 9 10 | public static String valueOfOrDefault(String myValue) { //replace space with underscore so it matches enum name String value=myValue.toUpperCase().replaceAll("\\\\s","_"); for(myEnum type : myEnum.class.getEnumConstants()) { if(type.name().equalsIgnoreCase(value)) { return type.toString(); } } return myValue; } |
1 2 3 4 5 6 7 | public enum myEnum { india, russian, england, north_america } |
访问值
1 | myEnum.values() |
我还介绍了自己的valueOf方法。但我返回的是Enum Type,而不必转换" checkString"
例如,checkString是" Petty Cash"。
JournalType journalType = JournalType.valueOfOrDefault(checkString);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public enum JournalType { MISC_BILLING("Misc Billing"), MISC_BILLING_AUTONUM("Misc Billing AutoNum"), PRETTY_CASH("Petty Cash"), RECURRING_AP("Recurring AP"), GL_JOURNAL("GL Journal"), GDS_NDS("GDS/NDS"); private final String journalType; JournalType(String journalType) { this.journalType = journalType; } @Override public String toString() { return journalType; } public static JournalType valueOfOrDefault(String myValue) { for(JournalType type : JournalType.class.getEnumConstants()) { if(type.toString().equals(myValue)) { return type; } } throw new IllegalArgumentException("JournalType not found"); } } |