0
public static enum pie {
APPLE_PIE1(1, 250),
PUMPKIN_PIE1(2, 300),
OTHER_PIE1(3, 350),
APPLE_PIE2(4, 400),
OTHER_PIE2(5, 450),
PUMPKIN_PIE2(6, 500),
APPLE_PIE3(7, 550),
;
private static Map<Integer, pie> pie = new HashMap<Integer, pie>();
static {
for(pie pie : pie.values()) {
pie.put(pie.getId(), pie);
}
}
public static pie forId(int id) {
return pie.get(id);
}
private pie(int id, double exp) {
this.id = id;
this.exp = exp;
}
public int id;
public double exp;
System.out.println("My favorite type of pie is " + pie.toString().toLowerCase().replaceAll("_", " ").replaceAll("2", "").replaceAll("3", "").replaceAll("4", "") + ".");
示例代码撕掉了,但我怎么会作出这样的“.replaceAll(‘号’,‘’)”取代小于或等于所有的数字4用更简单的代码?创建一个新的.replaceAll 3次是多余的。如果我这样做...:Java的替换所有的字符串,替换所有数字低于3
double number[] = {1, 2, 3, 4};
pie.toString().toLowerCase().replaceAll("_", " ").replaceAll(number.toString(), "") + ".");
...我会得到一个讨厌的错误。
这里的错误...
SEVERE: An error occurred in an executor service! The server will be halted immediately.
java.util.regex.PatternSyntaxException: Unclosed character class near index 9
[[email protected]
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.clazz(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
显示你希望我们的错误 - 自己运行程序? – MrSmith42
你确定你使用正确的语法吗?最后一行似乎以一个随机的')'结尾,我不认为它应该在那里。 – Patrickdev