如何在switch语句中将字符串与枚举进行比较?

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

How to compare a string with an Enum in a switch?

问题

我有一个如下的枚举:

public enum TripType {
  TRIP1,
  TRIP2,
  TRIP3,
  TRIP4
}

然后我有一个方法,接收一个字符串参数 setupTravel(String tripType),我需要将 tripType 的值与 TripType 进行比较。我希望使用如下的 switch-case 结构:

void setupTravel(String tripType){
    switch (tripType){
      case TripType.TRIP1.toString():
        setup1();
        break;
      case TripType.TRIP2.toString():
        setup2();
        break;
    }
}

但是,在 TripType.TRIP1.toString() 这一行会出现以下错误:
需要常量表达式

我该如何修复这个问题呢?

英文:

i have an enum like below:

public enum TripType {
  TRIP1,
  TRIP2,
  TRIP3,
  TRIP4
}

Then i have a method which receives a string parameter setupTravel(String tripType) and i need to check the value of tripType with a TripType. I wish to use a switch-case as below:

setupTravel(String tripType){
    switch (tripType){
      case TripType.TRIP1.toString():
        setup1();
        break;
      case TripType.TRIP2.toString():
        setup2();
        break;
    }
}

But, in the line TripType.TRIP1.toString() it complains with:
Constant expression required

How can i fix it?

答案1

得分: 3

<!-- 语言: lang-java -->

setupTravel(String tripType) {
  try {
    switch (TripType.valueOf(tripType)) {
      case TRIP1:
        setup1();
        break;
      case TRIP2:
        setup2();
        break;
    }
  } catch (IllegalArgumentException ex) {
    // 在此处理无效的行程类型
  }
}
英文:

<!-- language: lang-java -->

setupTravel(String tripType) {
  try {
    switch (TripType.valueOf(tripType)) {
      case TRIP1:
        setup1();
        break;
      case TRIP2:
        setup2();
        break;
    }
  } catch (IllegalArgumentException ex) {
    // Handle invalid trip type here
  }
}

答案2

得分: 0

由于您的方法似乎是根据枚举字符串进行切换,为什么不直接使用枚举呢?除非您在switch块中包含了所有的枚举值,否则可能希望添加一个default

setupTravel(TripType type){
    switch (type){
      case TRIP1:
        setup1();
        break;
      case TRIP2:
        setup2();
        break;
    }
}

调用方法如下:

setupTravel(TripType.TRIP1);
英文:

Since your method seems to switch on the Enum string, why not just use the Enum? You may want to include a default unless you have all Enums in the switch block.

setupTravel(TripType type){
    switch (type){
      case TRIP1:
        setup1();
        break;
      case TRIP2:
        setup2();
        break;
    }
}

You would call it as

setupTravel(TripType.TRIP1)

</details>



# 答案3
**得分**: 0

如果对您有用的话,您可以使用枚举构造函数来替代使用 switch case。类似这样:

```java
public enum TripType {
    TRIP1("setup1"), TRIP2("setup2");
    String setup;
    private TripType(String setup) {
        this.setup = setup;
    }
    public String getSetup() {
        return setup;
    }
    public static TripType getTripTypeByName(String name) {
        return Arrays.stream(TripType.values()).filter(t -> t.name().equalsIgnoreCase(name)).findAny().orElse(null);
    }
}

根据名称获取枚举,您可以这样使用:

TripType tripType = TripType.getTripTypeByName("trip1");
System.out.println(tripType != null ? tripType.getSetup() : null);
英文:

If it will be useful for you, You can use an enum constructor instead of using a switch case.
Something like this:

public enum TripType {
	TRIP1(&quot;setup1&quot;), TRIP2(&quot;setup2&quot;);
	String setup;
	private TripType(String setup) {
		this.setup = setup;
	}
	public String getSetup() {
		return setup;
	}
	public static TripType getTripTypeByName(String name) {
		return Arrays.stream(TripType.values()).filter(t -&gt; t.name().equalsIgnoreCase(name)).findAny().orElse(null);
	}
}

And getting enum based on the name, you can use like this:

TripType tripType = TripType.getTripTypeByName(&quot;trip1&quot;);
System.out.println(tripType != null ? tripType.getSetup() : null);

huangapple
  • 本文由 发表于 2020年10月27日 22:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64556316.html
匿名

发表评论

匿名网友

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

确定