Pregunta

I have defined this enum :

public enum UsageType {

    START("start"),
    PAUSE("pause"),
    RESUME("resume"),
    STOP("stop"),
    DESTROY("destroy");

    private final String mType;

    private UsageType(String type) {
        mType = type;
    }

    /**
     * Get the string representation of the UsageType
     * @return string representation of the UsageType
     */
    public String getAsText() {
        return mType;
    }
}

In another class, I have a constractor that takes string, and I want to make an enum with that string:

public class AppUsage {

    private String mActivityName;
    private String mFormattedTime;
    private UsageType mUsageType;

    public AppUsage(String activityName, String formattedTime, String usageType) {
        mActivityName = activityName;
        mFormattedTime = formattedTime;
        mUsageType =  mUsageType.valueOf(usageType); //HERE STRING TO ENUM!
    }

    //Setters and Getters....

Here is the error I get:

java.lang.IllegalArgumentException: start is not a constant in com.embedonix.mobilehealth.serverwork.usage.UsageType
        at java.lang.Enum.valueOf(Enum.java:198)
        at com.embedonix.mobilehealth.serverwork.usage.UsageType.valueOf(UsageType.java:6)
¿Fue útil?

Solución

Try UsageType.valueOf(usageType.toUpperCase()). When you use that method, the string should match the constant name (START) and the case matters.

Note that I am using the enum name UsageType because valueOf is a static method. So, you should need an instance there.

Also, note that the valueOf method throws IllegalArgumentException, runtime excpetion, if no constant with the name exists.

Otros consejos

If uses the constant name type (the one in upper case), not the internal name you pass by parameter on the constructor.

String str = "START"; // as example...could be "PAUSE", or "DESTROY", etc.
UsageType type = UsageType.valueOf(str);

Remove this constructor parameter..it's useless. Make it simply like this:

public enum UsageType {
   START,
   PAUSE,
   RESUME,
   STOP,
   DESTROY;
}

and for the getAsText() just use the built in UsageType.name().

What about this?

    public AppUsage(String activityName, String formattedTime, String usageType) {
      mActivityName = activityName;
      mFormattedTime = formattedTime;
      for(UsageType type : UsageType.values())
        {
          if(type.getAsText().equals(usageType))
            mUsageType = type;
        }
      }

when i want to convert string to enum, im always using lookup map add this to your enum

private static final Map<String, UsageType> lookup = new HashMap<String, UsageType>();
    static {
        for (final UsageType s : EnumSet.allOf(UsageType.class)) {
            lookup.put(s.getAsText(), s);
        }
    }
    static public UsageType fromString(final String name) {
        return lookup.get(name);
    }

now to convert your string to enum all what you need to do is UsageType.fromString("someString");

What i have observed is based on enum we can get value but not by passing value to get enum name. i.e. Try this it is working fine for me. Hope it should helpful for you.

public enum UsageType {    
    START("start"),
    PAUSE("pause"),
    RESUME("resume"),
    STOP("stop"),
    DESTROY("destroy");

    private final String mType;

    UsageType(String type) {
        mType = type;
    }


    public String getAsText() {
        return mType;
    }

}

public class AppUsage {

    private String mActivityName;
    private String mFormattedTime;
    public static UsageType mUsageType;

    public AppUsage(String activityName, String formattedTime, String usageType) {
        mActivityName = activityName;
        mFormattedTime = formattedTime;
        mUsageType =  UsageType.valueOf(usageType); //HERE STRING TO ENUM!
    }

    public String getmActivityName() {
        return mActivityName;
    }

    public void setmActivityName(String mActivityName) {
        this.mActivityName = mActivityName;
    }

    public String getmFormattedTime() {
        return mFormattedTime;
    }

    public void setmFormattedTime(String mFormattedTime) {
        this.mFormattedTime = mFormattedTime;
    }

    public UsageType getmUsageType() {
        return mUsageType;
    }

    public void setmUsageType(UsageType mUsageType) {
        AppUsage.mUsageType = mUsageType;
    }
 public static void main(String args[]){
//   AppUsage au=new AppUsage("a", "a","START");
     UsageType v[]=mUsageType.values();
     UsageType u=mUsageType.valueOf("START");
     System.out.println(u.getAsText());

 }
}

hope it may solve your problem

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top