如何检查枚举值并返回相应的枚举
我的架构如下所示,
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道您在这里要做什么,但您误解了枚举的概念。在评论中,您说状态代码是一个枚举,但不是一个枚举,它是JSON格式而不是真正的Java表达式。我说我不知道您要实现什么。
首先创建枚举
字符串,请
,然后使用以下代码修改您的方法
如果要获取相当于枚举值的
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
if you want to get the string equivalent of the enum values you use the following code
and then modify your method like this
you can get the string value of an enum as
您似乎对枚举在Java的工作方式感到困惑。
enum
基本上是可以分配给枚举类实例的知名值的列表。它涵盖了从/从/频繁需要的字符串转换的方法。enum
是所有人的超级阶级,但是您通常不需要直接使用它。例如,假设我有以下定义:
这意味着类
statuscode
的对象只能具有值a
,t
或u
。要转换,我将使用:
在这里了解更多信息: 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:
This means that an object of the class
StatusCode
can only have the valuesA
,T
orU
.And to convert, I'd use :
Learn more here: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html