Question

I have class with inner enum:

public class Apartment implements Comparable<Apartment> {
    // more properties
    private StarRating rating;
    private SleepPlaces sleepPlaces;


    public enum StarRating {
        TWO_STARS("two stars"), THREE_STARS("three stars"), FOUR_STARS("four stars"), FIVE_STARS("five stars");

        private String description;

        private StarRating(String description) {
            this.description = description;
        }
        public String getDescription() {
            return description;
        }
    }

    public void setRating(StarRating rating) {
        this.rating = rating;
    }

As you can see it has description value, which describes this enum element.

And I need to set enum from jsp selection field. Skip some jsp code we have some string value, as - two stars, have chosen by user.

And I want to set it to Apartment object, but I can't do this with enum's valueOf(). It throws IllegalArgumentException.

Some example:

public class Test {
    public static void main(String[] args) {
        Apartment app = new Apartment();

        String rating2 = "TWO_STARS";
        app.setRating(Enum.valueOf(Apartment.StarRating.class, rating2));
        System.out.println("here is rating: " + app.getRating());

        String rating = "two stars";
        app.setRating(Enum.valueOf(Apartment.StarRating.class, rating));
        System.out.println("here is rating: " + app.getRating());

    }
}

Output:

here is rating: TWO_STARS
Exception in thread "main" java.lang.IllegalArgumentException: No enum constant com.lelyak.model.Apartment.StarRating.two stars
    at java.lang.Enum.valueOf(Enum.java:236)
    at com.lelyak.application.Test.main(Test.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

How to implement second variant for rating value? Any suggestions?

How to set to enum element by enum's value?

SOLUTION:

add static method to enum:

    public static StarRating getByDescription(String description){
        if(description != null){
            for(StarRating sr : StarRating.values()){
                if(description.equalsIgnoreCase(sr.description)){
                    return sr;
                }
            }
        }
        return null;
    }
Was it helpful?

Solution

I think what you're actually asking is how to set enum by description. There's no built-in construct that lets you do this, but you can add a static method to your enumeration:

public static StarRating getByDescription(String description){
    if(description == null){
        return null;
    }
    for(StarRating sr : StarRating.values()){
        if(description.equals(sr.description)){
            return sr;
        }
    }
    return null;
}

OTHER TIPS

You can't use method 'Enum.valueOf' to resolve Enum by description. you can write your own method in 'Apartment' class to resolve it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top