30

In my Java Program have Enum class like..

public enum DemoType{
DAILY, WEEKLY, MONTHLY;
 }

And in my jsp i'm taking values from user like select box and this Jsp called like DemoJspBean ..

<form:select path="repeatWeektype">
    <form:option value="DAILY" />
    <form:option value="WEEKLY" />
    <form:option value="MONTHLY" />
</form:select>

My HibernateVO class is ..

public class DemoVO{
  @Column(name = "REPEAT_TYPE")
  @Enumerated(EnumType.STRING)
  private RepeatType repeatType;
}

Now i want to insert this value into DB using Hibernate Bean(setter and getter)

DemoVO demo = new DemoVO();
demo.setRepeatType(demoJspBean.getRepeatWeektype());

but it is show error..

So how to convert my String value into enum class type?

1

5 Answers 5

59

Use the valueOf method on the Enum class.

DemoType demoType =   DemoType.valueOf("DAILY")

It'll throw an IllegalArgumentException should the string argument provided be invalid. Using your example

DemoType demoType =  DemoType.valueOf("HOURLY");

The line above will throw an IllegalArgumentException because HOURLY is not part of your DemoType

Sign up to request clarification or add additional context in comments.

2 Comments

please.. i want some more clarification.
If the drop down on the UI is populated using the Enum then this should not be a problem. However as a practice one needs to add validation on the data received from the UI before performing the business logic.
9

This may help you to understand how enum types work.

Say, This is my enum class.

public enum GetDate {

SUNDAY("1"), MONDAY("2"), TUESDAY("3"), WEDNESDAY("4"), THURSDAY("5"), FRIDAY("6"), SATURDAY("7");
private String code;

private GetDate(String code) {
    this.code = code;
}

public String getCode() {
    return code;
}

public static GetDate getEnum(String code) {

    switch (code) {
        case "1":
            return SUNDAY;
        case "2":
            return MONDAY;
        case "3":
            return TUESDAY;
        case "4":
            return WEDNESDAY;
        case "5":
            return THURSDAY;
        case "6":
            return FRIDAY;
        case "7":
            return SATURDAY;
        default:
            return null;
     }
   }
 }

Following shows how my enum works

public class MyClass {
public static void main(String[] args) {
    System.out.println("Sunday enum value " + GetDate.SUNDAY);  // enum SUNDAY
    System.out.println("Name of the day assign to 1 " + GetDate.getEnum("1"));  // enum SUNDAY
    System.out.println("Sunday enum value " + GetDate.valueOf("SUNDAY").getCode()); // String code of SUNDAY
    System.out.println("Sunday enum value " + GetDate.valueOf("SUNDAY"));// enum Sunday
   }
}

1 Comment

It works. But it is too heavy weight. If so, I cannot see the benefit of enum compared to plain class.
4

If for some reason you use a value that does not exist in the enum (using the method DemoType.valueOf(), you'll get an java.lang.IllegalArgumentException. Hey! Wait!, you can iterate into the values:

public static void main(String[] args) {
    System.out.println(DemoType.convert("DAILY"));
    System.out.println(DemoType.convert("YEARLY"));
}

enum DemoType {
    DAILY, WEEKLY, MONTHLY;
    public static DemoType convert(String str) {
        for (DemoType demoType : DemoType.values()) {
            if (demoType.toString().equals(str)) {
                return demoType;
            }
        }
        return null;
    }
}

The output:

DAILY
null

1 Comment

Returning null propagates null checking throughout the API which can easily be avoided. Instead add a UNKNOWN value to the enum to be returned in the unmatched case.
1

Using Spring's TypeConverterSupport you can resolve string property to enum instance like that:

@Value("${enum.type}") 
MyEnum enum;

Comments

0

You can user DemoType.valueOf() method by passing the string, which will convert it to the DemoType enum corresponding to the string. Make sure that the String value is the same as declared enum. For example

    DemoType dt = DemoType.valueOf("DAILY")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.