String to Enum
转换的所有示例只占用一个字符串但在我的示例字符串中就像这样......
String allDays="MONDAY,SUNDAY,FRIDAY";
和My Enum Class就是这样..
public enum WeekdayType {
MONDAY(Calendar.MONDAY), TUESDAY(Calendar.TUESDAY), WEDNESDAY(
Calendar.WEDNESDAY), THURSDAY(Calendar.THURSDAY), FRIDAY(
Calendar.FRIDAY), SATURDAY(Calendar.SATURDAY), SUNDAY(
Calendar.SUNDAY);
private int day;
private WeekdayType(int day) {
this.day = day;
}
public int getDay() {
return day;
}
}
所以在那个时间WeedayType.valueOf(allDay)
给出错误.....
对此有任何建议..
答案 0 :(得分:2)
试试这样:
String allDays = "MONDAY,SUNDAY,FRIDAY";
for (String day : allDays.split(",")) {
System.out.println(WeekdayType.valueOf(day));
}
答案 1 :(得分:1)
您将收到以下错误
java.lang.IllegalArgumentException: No enum const class com.java.core.Test$WeekdayType.MONDAY,SUNDAY,FRIDAY
原因是您将以下字符串传递给 WeedayType 枚举,这是一个非法参数,如异常所示。
String allDays = "MONDAY,SUNDAY,FRIDAY";
WeedayType.valueOf(allDay);
您可以传递给valueOf方法的有效值是“MONDAY”,“TUESDAY”等(即您的枚举名称)。其他值为您提供java.lang.IllegalArgumentException,这是正确的行为。