我的枚举问题。
这是:
public enum DataEnum {
NAME_PEOPLE("NAME_PEOPLE"),
FIRSTNAME_PEOPLE("FIRSTNAME_PEOPLE"),
ID("ID"),
PASS("PASS"),
NEW_MAIL("NEW_MAIL");
private String name;
private DataEnum(String s) {
name = s;
}
public String getValue() {
return name;
}
public void setValue(String s) {
this.name = s;
}
}
我在那里使用它:
public String transform(String textToTransform, People people){
Pattern TAG_REGEX = Pattern.compile("#(.+?)#");
Matcher matcher = TAG_REGEX.matcher(textToTransform);
while (matcher.find()) {
String s = matcher.group(1);
switch (s) {
case "FIRSTNAME_PEOPLE":
DataEnum.valueOf(s).setValue(people.getFirstName());
break;
case "NAME_PEOPLE":
DataEnum.valueOf(s).setValue(people.getName());
break;
case "ID":
DataEnum.valueOf(s).setValue(people.getEmail());
break;
case "PASS":
DataEnum.valueOf(s).setValue(people.getPassword());
break;
default:
break;
}
textToTransform = textToTransform.replace("#" + DataEnum.valueOf(s) + "#", DataEnum.valueOf(s).getValue());
}
return textToTransform;
}
我收到以下错误:
引起:java.lang.IllegalArgumentException:没有枚举常量 fr.pdf.utils.DataEnum.FIRSTNAME_PEOPLE
编辑:
引起:java.lang.IllegalArgumentException:没有枚举常量 fr.pdf.utils.DataEnum.FIRSTNAME_PEOPLE at java.lang.Enum.valueOf(Enum.java:238)at fr.pdf.utils.DataEnum.valueOf(DataEnum.java:3) 在 fr.pdf.services.impl.MailServiceImpl.transform(MailServiceImpl.java:160) 在 fr.pdf.services.impl.MailServiceImpl.sendMail(MailServiceImpl.java:84) 在 fr.pdf.dao.impl.People.update(People.java:372)
第160行对应于:
textToTransform = textToTransform.replace("#" + DataEnum.valueOf(s) + "#", DataEnum.valueOf(s).getValue());
答案 0 :(得分:0)
删除枚举并执行此操作:)
public String transform(String textToTransform, People people){
Pattern TAG_REGEX = Pattern.compile("#(.+?)#");
Matcher matcher = TAG_REGEX.matcher(textToTransform);
while (matcher.find()) {
String s = matcher.group(1);
String replaceWith = null;
switch (s) {
case "FIRSTNAME_PEOPLE":
replaceWith = people.getFirstName();
break;
case "NAME_PEOPLE":
replaceWith = people.getName();
break;
case "ID":
replaceWith = people.getEmail();
break;
case "PASS":
replaceWith = people.getPassword();
break;
default:
break;
}
textToTransform = textToTransform.replace("#" + s + "#", replaceWith);
}
return textToTransform;
}