Not a constant in Enum

I use the enumeration with the switch case, but I get the following error:

FEED NEWS is not a constant in FragmentName

This is the string constant enum,

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 throws an exception in the changeTitle function, even the passed value is the same, so any help would be appreciated as I tried my best to solve this problem.

+5
source share
4 answers

Add this code to your listing.

 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)) // ^^^^^^^^ 
+7
source

valueOf

Returns the enumeration constant of the specified enumeration type using the specified name. The name must exactly match the identifier used to declare an enumeration constant in this type. (Extraneous spaces characters are not allowed.

What you want to do id gets an enumeration by member value so that you write a function to do this, 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 the switch housing, for example

  switch (FragmentName.fromString(title)) { 
+3
source

Create a method like this:

 public static FragmentName getFragmentNameByText(String text) { for (FragmentName fragment : values()) { if (fragment.text.equals(text)) { return fragment; } } return null; } 

and name it instead of valueOf ().

+2
source

You can change your function to compare string values ​​passed to:

 public void changeTitle(String title) { if(title.equals(FRAGMENT_NEWSFEED.toString())) { System.out.println("1"); } else if(title.equals(FRAGMENT_MESSAGES.toString())) { System.out.println("2"); } else if(title.equals(FRAGMENT_EVENTS.toString())) { System.out.println("3"); } else if(title.equals(FRAGMENT_WHOISAROUDNME.toString())) { System.out.println("4"); } else { // throw an error } } 

You cannot disable the function call, so you need to use the if-else block.

0
source

All Articles