英文:
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("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);
}
}
And getting enum based on the name, you can use like this:
TripType tripType = TripType.getTripTypeByName("trip1");
System.out.println(tripType != null ? tripType.getSetup() : null);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论