0

I have issues storing an enum value and then querying it via the toString() method. I require to handcode the numbers. How do I safely override my toString() ?

public enum Result {
        SUCCESS(0),
        INVALID_UPLOAD_KEY(-20),
        UPLOAD_KEY_NOT_FOUND(-80);

        private final int value;  //do I need this?

        private Result(int value) {
            this.value = value;
        }

        public static Result fromInt(final int n) {
            for (final Result result : values()) {
                if (result.getValue() == n) {
                    return result;
                }
            }
            return null;
        }


        public int getValue() {
            return this.value;
        }

        public String toString() {
            switch (this.value) {
                case UPLOAD_KEY_NOT_FOUND: //Type mismatch: cannot convert from MyClass.Result to int
                    return "Upload Key not found";
                case INVALID_UPLOAD_KEY: //Type mismatch: cannot convert from MyClass.Result to int
                    return "Invalid Upload Key";
                case SUCCESS: //Type mismatch: cannot convert from MyClass.Result to int
                    return "Success";
                default:
                    return "No result code associated with: " + this.value;
            }
        }
    }

Eclipse complains the toString method

Type mismatch: cannot convert from MyClass.Result to int
Nederealm
  • 447
  • 4
  • 15

4 Answers4

2

Others have explained why your switch statement is failing - and unless you need the numbers for some other reason, you can get rid of them. But you don't need the switch statement at all - just make the message an instance variable:

public enum Result {
    SUCCESS("Success"),
    INVALID_UPLOAD_KEY("Invalid Upload Key"),
    UPLOAD_KEY_NOT_FOUND("Upload Key not found");

    private final String message;

    private Result(String message) {
        this.message = message;
    }

    @Override public String toString() {
        return message;
    }
}

Of course if you still need the integer value for other reasons, it's fine to keep that as well.

I would be tempted not to override toString(), but to provide a getMessage() method instead, by the way.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • One reason I can think of overriding the toString method would be for adding the enum to a combo box, but that's about it. – Obicere Sep 25 '14 at 16:46
1

This

switch (this.value) {
case UPLOAD_KEY_NOT_FOUND: 
  return "Upload Key not found";
case INVALID_UPLOAD_KEY:
  return "Invalid Upload Key";
case SUCCESS:
  return "Success";
default:
  return "No result code associated with: " + this.value;
}

Should be

switch (this) {
case UPLOAD_KEY_NOT_FOUND: 
  return "Upload Key not found";
case INVALID_UPLOAD_KEY:
  return "Invalid Upload Key";
case SUCCESS:
  return "Success";
default:
  return "No result code associated with: " + this.value;
}

You are using the enum type in your case statements. You still need to numerical values you are assigning if need to use fromInt(int).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

When switching on enum values, you can use the enum itself in the switch statement. There is no need to look at your own custom value. You can try:

switch (this) {
   case UPLOAD_KEY_NOT_FOUND:
       return "Upload Key not found";
   case INVALID_UPLOAD_KEY:
       return "Invalid Upload Key";
   case SUCCESS:
       return "Success";
   default:
       return "No result code associated with: " + this.value;
 }
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • For your `toString` method, the numbers you have (0, -20, -80) aren't necessary. They appear to be necessary only in your `fromInt` method, for converting from an `int` to your `Result` type. – rgettman Sep 25 '14 at 16:40
  • I'm not sure. Is there a way to tell? I'm using someone else's code. – Nederealm Sep 25 '14 at 16:42
  • Do you need those specific numbers? The `ordinal()` method will return Java's own internal numbers, but you don't control what numbers they are. – rgettman Sep 25 '14 at 16:43
  • OK, it do appears that I need the numbers. The fromInt() was referenced elsewhere in the project. – Nederealm Sep 25 '14 at 16:47
0

switch expects always constant values (an int or constant string).

You can read more here

In your case you should do something like this:

switch (this) {
    case -80: 
        return "Upload Key not found";
    case -20:
        return "Invalid Upload Key";
    case 0:
        return "Success";
    default:
        return "No result code associated with: " + this.value;
}

Or do not use the switch statement and try something like this:

return WordUtils.capitilize(this.getName().replace("_"))

Where WordUtils is this library.