switch-case with a string and enum in java

huangapple go评论118阅读模式
英文:

switch-case with a string and enum in java

问题

  1. 我想在Java中使用switch-case检查具有枚举值的字符串所以我做了这样的操作
  2. public enum DemoEnumType {
  3. ALL(""),
  4. TOP("acb"),
  5. BOTTOM("def");
  6. private String code;
  7. DemoEnumType(String code) {
  8. this.code = code;
  9. }
  10. public String code() {
  11. return this.code;
  12. }
  13. }

当我运行这段代码时,它会抛出一个异常:

  1. public class Demo {
  2. public static void main(String[] args) {
  3. DemoEnumType typeValue = DemoEnumType.valueOf("acb");
  4. switch (typeValue){
  5. case ALL:
  6. System.out.print("匹配");
  7. case BOTTOM:
  8. System.out.print("匹配");
  9. case TOP:
  10. System.out.print("匹配");
  11. }
  12. }
  13. }

异常信息:

Exception in thread "main" java.lang.IllegalArgumentException: No enum constant package.DemoEnumType.acb.

  1. <details>
  2. <summary>英文:</summary>
  3. I wanted to check a string with the value of enum in java using switch-case, so I did like this:

public enum DemoEnumType {

  1. ALL(&quot;&quot;),
  2. TOP(&quot;acb&quot;),
  3. BOTTOM(&quot;def&quot;);
  4. private String code;
  5. DemoEnumType(String code) {
  6. this.code = code;
  7. }
  8. public String code() {
  9. return this.code;
  10. }

}

  1. and when I run this code it throws an exception:

public class Demo {
public static void main(String[] args) {
DemoEnumType typeValue = DemoEnumType.valueOf("acb");

  1. switch (typeValue){
  2. case ALL:
  3. System.out.print(&quot;match&quot;);
  4. case BOTTOM:
  5. System.out.print(&quot;match&quot;);
  6. case TOP:
  7. System.out.print(&quot;match&quot;);
  8. }
  9. }

}

  1. **Exeption:**
  2. &gt;Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: No enum constant package.DemoEnumType.acb.
  3. </details>
  4. # 答案1
  5. **得分**: 2
  6. ```java
  7. DemoEnumType typeValue = DemoEnumType.valueOf("acb");

没有具有值“acb”的枚举元素。如果没有具有给定名称的元素,Enum#valueOf 将抛出 IllegalArgumentException。您需要使用 ALLBOTTOMTOP

  1. DemoEnumType type = DemoEnumType.valueOf("ALL");

或者,您可以使用一个 String 到 DemoEnumType 的 Map 进行 O(1) 查找,并使用您提供的值。

  1. Map<String, DemoEnumType> valueToType = Stream.of(DemoEnumType.values())
  2. .collect(Collectors.toMap(DemoEnumType::code, Function.identity()));
  3. DemoEnumType type = valueToType.get("abc");
英文:
  1. DemoEnumType typeValue = DemoEnumType.valueOf(&quot;acb&quot;);

No enum element exists with the value acb. Enum#valueOf will throw an IllegalArgumentException if no element exists with the given name. You need to use ALL, BOTTOM, or TOP.

  1. DemoEnumType type = DemoEnumType.valueOf(&quot;ALL&quot;);

Alternatively, you could use a Map of String to DemoEnumType for O(1) lookup and use the values you've provided.

  1. Map&lt;String, DemoEnumType&gt; valueToType = Stream.of(DemoEnumType.values())
  2. .collect(Collectors.toMap(DemoEnumType::code, Function.identity());
  3. DemoEnumType type = valueToType.get(&quot;abc&quot;);

答案2

得分: 1

你的枚举成员是ALL、TOP和BOTTOM,而不是字符串值。你只能将它们传递给valueOf()方法。

要使用字符串值,你可以在枚举中创建一个方法,该方法接收一个字符串,并返回相应的枚举值。

英文:

Your Enum members are ALL, TOP and BOTTOM, not the string values. you can only pass then to valueOf().

To use the string values you can create a method in your Enum that receives a String and returns the appropriate Enum

huangapple
  • 本文由 发表于 2020年8月20日 20:29:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63505142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定