Java从字符串创建枚举
我有枚举类Java从字符串创建枚举,java,enums,Java,Enums,我有枚举类 public enum PaymentType { /** * This notify type we receive when user make first subscription payment. */ SUBSCRIPTION_NEW("subscr_signup"), /** * This notify type we receive when user make subscription payment for
public enum PaymentType {
/**
* This notify type we receive when user make first subscription payment.
*/
SUBSCRIPTION_NEW("subscr_signup"),
/**
* This notify type we receive when user make subscription payment for next
* month.
*/
SUBSCRIPTION_PAYMENT("subscr_payment"),
/**
* This notify type we receive when user cancel subscription from his paypal
* personal account.
*/
SUBSCRIPTION_CANCEL("subscr_cancel"),
/**
* In this case the user cannot change the amount or length of the
* subscription, they can however change the funding source or address
* associated with their account. Those actions will generate the
* subscr_modify IPN that we are receiving.
*/
SUBSCRIPTION_MODIFY("subscr_modify"),
/**
* Means that the subscription has expired, either because the subscriber
* cancelled it or it has a fixed term (implying a fixed number of payments)
* and it has now expired with no further payments being due. It is sent at
* the end of the term, instead of any payment that would otherwise have
* been due on that date.
*/
SUBSCRIPTION_EXPIRED("subscr_eot"),
/** User have no money on card, CVV error, another negative errors. */
SUBSCRIPTION_FAILED("subscr_failed");
private String type;
PaymentType(String type) {
this.type = type;
}
String getType() {
return type;
}
}
当我尝试创建枚举时:
PaymentType type = PaymentType.valueOf("subscr_signup");
Java向我抛出错误:
IllegalArgumentException occured : No enum constant models.PaymentType.subscr_signup
如何修复此问题?
valueOf
返回其标识符与您传入的字符串匹配的枚举项。例如:
PaymentType t = PaymentType.valueOf("SUBSCRIPTION_NEW");
如果要获取其
type
字段与给定字符串匹配的枚举项,请在PaymentType
上编写一个静态方法,该方法循环通过PaymentType.values()
并返回匹配项。枚举类.valueOf()
方法将传递给它的字符串与enum实例的name
属性(实例名称的实际值)进行比较,因此在您的示例中,这将是“SUBSCRIPTION\u NEW”
,而不是“subscr\u signup”
您需要编写一个静态方法,返回给定值的正确枚举实例。例如:
public static PaymentType byTypeString(String type) {
for (PaymentType instance : values()) {
if (instance.type.equals(type)) {
return instance;
}
}
throw new IllegalArgumentException("No PaymentType enum instance for type: " + type);
}
public static PaymentType fromString(String string) {
for (PaymentType pt : values()) {
if (pt.getType().equals(string)) {
return pt;
}
}
throw new NoSuchElementException("Element with string " + string + " has not been found");
}
添加并使用此方法
public static PaymentType parse(String type) {
for (PaymentType paymentType : PaymentType.values()) {
if (paymentType.getType().equals(type)) {
return paymentType;
}
}
return null; //or you can throw exception
}
枚举没有用于此操作的现成方法。您需要使用准确的字段名:
PaymentType.valueOf("SUBSCRIPTION_MODIFY");
或者编写自己的方法,例如:
public static PaymentType byTypeString(String type) {
for (PaymentType instance : values()) {
if (instance.type.equals(type)) {
return instance;
}
}
throw new IllegalArgumentException("No PaymentType enum instance for type: " + type);
}
public static PaymentType fromString(String string) {
for (PaymentType pt : values()) {
if (pt.getType().equals(string)) {
return pt;
}
}
throw new NoSuchElementException("Element with string " + string + " has not been found");
}
所以这个代码:
public static void main(String[] args) {
System.out.println(PaymentType.fromString("subscr_modify"));
}
印刷品:
SUBSCRIPTION_MODIFY
是枚举变量值,而不是枚举值。
因此,如果您想根据枚举的值名获取枚举,必须这样做:
PaymentType type = PaymentType.valueOf("SUBSCRIPTION_NEW");
我认为您必须在内部编写一个比较方法来创建额外的静态方法,该方法将搜索enum@wero这不是复制品。如果字符串不是相同的枚举值,则提供的示例不是解决方案。@ssh您阅读了提供的问题的第二个答案了吗?我不能使用它,因为我不能更改常量。您当然可以编写一个方法。为什么不呢?如果无法修改PaymentType枚举,只需使用getType()
访问器将其作为静态方法添加到其他地方,而不是直接使用type
。