如何检查枚举值并返回相应的枚举

发布于 2025-01-24 02:01:02 字数 729 浏览 0 评论 0原文

我的架构如下所示,

"statusCode": {  
            "type": "string",
            "enum": ["A", "T", "U"]
        }

我试图编写一种可以检查代码并返回相应枚举的方法。

private void updateStatusCode(Event event) {
        Enum code = null;
        switch(event.getStatusCode()) {
            case A:
            code = ["A"];
            break;
            case T:
            break;
            case U:
            break;
            default:

        }
         return code;

    }

event.getStatusCode有效值为:A,T,U。现在,我需要检查这些代码并根据代码返回枚举。我尝试了以上,但它在代码= [“ a”]上给了我错误。下面的错误状态。

Syntax error on token "=", Expression expected after this token

我该如何解决?我是Java的新手。任何帮助将受到赞赏,谢谢

my schema is as below

"statusCode": {  
            "type": "string",
            "enum": ["A", "T", "U"]
        }

I am trying to write a method which would check for the code and return corresponding enum.

private void updateStatusCode(Event event) {
        Enum code = null;
        switch(event.getStatusCode()) {
            case A:
            code = ["A"];
            break;
            case T:
            break;
            case U:
            break;
            default:

        }
         return code;

    }

event.getStatusCode valid values are: A , T , U. Now I need to check for these codes and return enum based on the codes. I tried the above but it gives me error on code = ["A"]. error states below.

Syntax error on token "=", Expression expected after this token

How do i fix this ? I am new to java. any help is appreciated, Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈年往事 2025-01-31 02:01:02

我不知道您在这里要做什么,但您误解了枚举的概念。在评论中,您说状态代码是一个枚举,但不是一个枚举,它是JSON格式而不是真正的Java表达式。我说我不知道​​您要实现什么。

首先创建枚举

enum StatusCode{
   A,
   B,
   U

}

字符串,请

enum StatusCode{
   A("A"),
   T("T"),
   U("U");
   String code;
   public StatusCode(String code){
     this.code=code;
   }
   String getCode(){
     return code;
   }
  }

,然后使用以下代码修改您的方法

private void updateStatusCode(Event event) {
        StatusCode code;
        switch(event.getStatusCode()) {
            case A:
            code = StatusCode.A;
            break;
            case T:
            code = StatusCode.T;
            break;
            case U:
            code = StatusCode.U;
            break;
        }
         return code;

    }

如果要获取相当于枚举值的

StatusCode code=StatusCode.A;
String strCode=code.getCode();

I don't know what you are trying to do here but you misunderstood the concept of enum. In the comment you said statusCode is an enum but it isn't an enum it's in a json format and not a real Java expression.As I said I don't know what you are trying to achieve but you can modify your code like below .

first create an enum

enum StatusCode{
   A,
   B,
   U

}

if you want to get the string equivalent of the enum values you use the following code

enum StatusCode{
   A("A"),
   T("T"),
   U("U");
   String code;
   public StatusCode(String code){
     this.code=code;
   }
   String getCode(){
     return code;
   }
  }

and then modify your method like this

private void updateStatusCode(Event event) {
        StatusCode code;
        switch(event.getStatusCode()) {
            case A:
            code = StatusCode.A;
            break;
            case T:
            code = StatusCode.T;
            break;
            case U:
            code = StatusCode.U;
            break;
        }
         return code;

    }

you can get the string value of an enum as

StatusCode code=StatusCode.A;
String strCode=code.getCode();

梨涡 2025-01-31 02:01:02

您似乎对枚举在Java的工作方式感到困惑。 enum基本上是可以分配给枚举类实例的知名值的列表。它涵盖了从/从/频繁需要的字符串转换的方法。 enum是所有人的超级阶级,但是您通常不需要直接使用它。

例如,假设我有以下定义:

enum StatusCode {
 A,
 T,
 U
}

这意味着类statuscode的对象只能具有值atu
要转换,我将使用:

String fromString = "A";
StatusCode sc = StatusCode.valueOf(fromString); //gives sc = StatusCode.A
String backToString = sc.name(); //gives backToString = "A"

在这里了解更多信息: https:/ /docs.oracle.com/javase/tutorial/java/javaoo/enum.html

You seem confused about how enumerations works in Java. An enum is basically a list of well-known values that can be assigned to an instance of the enum class. It encompasses the methods to convert from/to String as frequently required. Enum is the superclass of them all, but you usually don't need to use it directly.

For instance, let's say I've got the following definition:

enum StatusCode {
 A,
 T,
 U
}

This means that an object of the class StatusCode can only have the values A, T or U.
And to convert, I'd use :

String fromString = "A";
StatusCode sc = StatusCode.valueOf(fromString); //gives sc = StatusCode.A
String backToString = sc.name(); //gives backToString = "A"

Learn more here: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文