Hey guys, I have a quick question. If given a string Value how can i get the corresponding int value in my enum class?
Ex:
Given a string “Ordinary” I want the value 0 returned. Here is what I have:
public enum MembershipTypes
{
ORDINARY(0,"Ordinary"),
WORKING(1,"Working"),
CORE(2,"Core"),
COORDINATOR(3,"Coordinator");
private int intVal;
private String strVal;
/**
*
* @param intValIn
* @param strValIn
*/
MembershipTypes(int intValIn, String strValIn)
{
intVal = intValIn;
strVal = strValIn;
}
/**
*
* Gets the integer value
* @return intVal
*/
public int getIntVal()
{
return intVal;
}
/**
*
* Gets the string value
* @return strVal
*/
public String getStrVal()
{
return strVal;
}
}
Here’s the simplest way to do it. Add this static method to your enum: