Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string value into Enum in Java?

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?

like image 711
java Avatar asked Jul 05 '13 04:07

java


People also ask

How do I convert string to enum?

Use the Enum. IsDefined() method to check if a given string name or integer value is defined in a specified enumeration. Thus, the conversion of String to Enum can be implemented using the Enum. Parse ( ) and Enum.

Can we convert enum to string?

We can convert an enum to string by calling the ToString() method of an Enum.

What does enum valueOf return?

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.)


2 Answers

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

like image 57
kolossus Avatar answered Sep 21 '22 20:09

kolossus


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    } } 
like image 27
Ruchira Gayan Ranaweera Avatar answered Sep 21 '22 20:09

Ruchira Gayan Ranaweera