Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not a constant in Enum

I am using enumeration with switch case but I am getting the following error:

NEWS FEED is not a constant in FragmentName

This is my enum string constant,

 public enum FragmentName{
        FRAGMENT_NEWSFEED("NEWS FEED"),
        FRAGMENT_MESSAGES("MESSAGES"),
        FRAGMENT_EVENTS("EVENTS"),
        FRAGMENT_WHOISAROUDNME("WHOS AROUND");

        private final String text;
        private FragmentName(final String text) {
            this.text = text;
        }
        @Override
        public String toString() {
            return text;
        }
    }

//This is my function from where i check for corresponding enum constant

 public void changeTitle(String title) {
        switch (Enums_String.FragmentName.valueOf(title)) {
            case FRAGMENT_NEWSFEED:
                System.out.println("1");
                break;
            case FRAGMENT_EVENTS:
                System.out.println("2");
                break;
            case FRAGMENT_MESSAGES:
                System.out.println("3");
                break;
            case FRAGMENT_WHOISAROUDNME:
                System.out.println("4");
                break;
        }
    }

When I call

     changeTitle("NEWS FEED");

it creates an exception in the changeTitle function even the value passed is same, so any help would be appreciated as I have tried my every effort to solve this.

like image 876
Reprator Avatar asked Jul 07 '15 14:07

Reprator


People also ask

What are enum constants?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Should enums be constants?

Enums are strongly-typed constants that make the code more readable and less prone to errors. Enums are known as named constants. If we have some constants related to each other then we can use an enum to group all the constants.

Are enums just constants?

Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.

Can we add constants to enum?

Using enums, we can define and use our constants in the way of type safety. It brings compile-time checking to the constants. Further, it allows us to use the constants in the switch-case statement.


2 Answers

Add this code to your enum

private static final Map<String, FragmentName> map = new HashMap<>();
static {
    for (FragmentName en : values()) {
        map.put(en.text, en);
    }
}

public static FragmentName valueFor(String name) {
    return map.get(name);
}

Now instead of valueOf use valueFor

switch (Enums_String.FragmentName.valueFor(title))
//                                ^^^^^^^^
like image 90
Pshemo Avatar answered Nov 15 '22 00:11

Pshemo


The valueOf

Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.

What you want do id get the enum by a member value for that you have write a function to do so like fromString below

 public enum FragmentName {

    FRAGMENT_NEWSFEED("NEWS FEED"),
    FRAGMENT_MESSAGES("MESSAGES"),
    FRAGMENT_EVENTS("EVENTS"),
    FRAGMENT_WHOISAROUDNME("WHOS AROUND");

    private final String text;

    private FragmentName(final String text) {
        this.text = text;
    }

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

    public static FragmentName fromString(String value) {
        for (FragmentName fname : values()) {
            if (fname.text.equals(value)) {
                return fname;
            }
        }
        return null;
    }
}

and replace your switch case like

    switch (FragmentName.fromString(title)) {
like image 22
Madhan Avatar answered Nov 15 '22 01:11

Madhan