如何创建解析函数
我有类错误。 我有值错误代码。 我需要通过错误代码获取字符串。
例如,int number = -2;
Errors.Parse(number).toString();
返回值#-2#用户名已被使用。
public enum Errors {
UNKNOWN_ERROR(-1, "Unknown error"),
LOGIN_ALREADY_EXISTS(-2, "The username is already in use."),
LOGIN_OR_PASSWORD_NOT_CORRECT (-3, "LOGIN_OR_PASSWORD_NOT_CORRECT"),
NULL_POINTER (-4,"Null pointer exception"),
NOT_CORRECT_DATA (-5,"NOT_CORRECT_DATA");
private final int errorCode;
private final String message;
private Errors(int errorCode, String message) {
this.errorCode = errorCode;
this.message = message;
}
public int getErrorCode() {
return errorCode;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "#" + errorCode + "#" + message;
}
}
I have class Errors.
I have value errorCode.
I need get string by errorCode.
example, int number = -2;
Errors.Parse(number).toString();
return value #-2#The username is already in use.
public enum Errors {
UNKNOWN_ERROR(-1, "Unknown error"),
LOGIN_ALREADY_EXISTS(-2, "The username is already in use."),
LOGIN_OR_PASSWORD_NOT_CORRECT (-3, "LOGIN_OR_PASSWORD_NOT_CORRECT"),
NULL_POINTER (-4,"Null pointer exception"),
NOT_CORRECT_DATA (-5,"NOT_CORRECT_DATA");
private final int errorCode;
private final String message;
private Errors(int errorCode, String message) {
this.errorCode = errorCode;
this.message = message;
}
public int getErrorCode() {
return errorCode;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "#" + errorCode + "#" + message;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须循环枚举的值,并根据给定的错误代码(num)单独检查每个值的错误代码。
像这样的东西:
You will have to loop the values of your enum and check individually the error code of each against the given error code (num).
something like:
您可以在
Errors
枚举中为其创建一个Map
。使用错误代码查找Errors
枚举。通过进行如下调用来查找值:Errors.getErrorForCode(1);
You can create a
Map
for that inside yourErrors
enum. Use the error code to look up theErrors
enum. Look up the value by making a call like this:Errors.getErrorForCode(1);
添加类似这样的内容(特别是如果经常使用 parse 方法):
Add something like this (especially if the parse method is used frequently):