Java Enum Question

Hi Everyone,
I'm trying to utilize an enum to dynamically generate error messages based an error code which is recieved.  Here is an example of what I trying to do:

enum errorMsgs{
                 error_code1(1),
                 error_code(2);
                private long errorCode
                errorMsgs(long errorCode)
               {
                     this.errorCode = errorCode;
                }

              pulic String getErrorMsg(long errorCode)
             {
                        switch(this)
                        case error_code1: return "Message for error code 1";
                        case error_code2: return "Message for error code 2";
                        default: return "default error";

              }
}

enum variable defined in class
errorMsgs _errors;

Elsewhere in the class:

long errorcode = connect.getErrorCode();
// Here is the question, how can I instantiate the enum class to dynamically select the errorcode such
// that the following is dynamic:
_error.error_code1;

// This method always returns the error code set above, which in this example is basically hard
// coded to error_code1, I need it to retrieve the message retrieved from connect.getErrorCode();
String errorMessage = _error.getErrorMessages(errorcode);


Doesa anyone have any ideas how I can do this with enums?  I would use the switch statement on a long but this is the JDK 1.5 and it doesn't allow switch statemenst on longs only on enums.  I thought it would be good to explore the enum path as I've never used them before.  Let me know if I need to clarify anything.  Thanks.








LVL 1
zozigAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ajay-SinghCommented:
> how can I instantiate the enum class to dynamically select the errorcode
such
 
You can do something like this:
 
errorMsgs _errors = errorMsgs.get(1);
You need to implment a method to get the errorMsgs
 
So the enum class would look like
 
enum errorMsgs{
                 error_code1(1),
                 error_code(2);
                private long errorCode
                errorMsgs(long errorCode)
               {
                     this.errorCode = errorCode;
                }

              pulic String getErrorMsg(long errorCode)
             {
                        switch(this)
                        case error_code1: return "Message for error code 1";
                        case error_code2: return "Message for error code 2";

                        default: return "default error";

              }
              pulic errorMsgs getErrorMsg(long errorCode)
             {
                    for(errorMsgs x: errorMsgs.values()) {
                        if(x._errorCode==errorCode) {
                            return x;
                        }
                    }
                   return null;
              }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zozigAuthor Commented:
Hi Ajay,
Thanks for the response, however when I call get on the following code:

errorMsgs _errors = errorMsgs.get(1);

It cannot find the get method. I implemented the getErrorMsg method in my enum class but it stil can't find the get method.  Is that a standard method for enums?

0
Ajay-SinghCommented:
sorry, typo

change this

pulic errorMsgs getErrorMsg(long errorCode)

to

pulic errorMsgs get(long errorCode)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.