Top community members

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

[Edit]
EN

Java - convert String to Enum object

6 points
Created by:
JustMike
3364

Short solution:

public class Example1 {

    enum ColorEnum {
        RED,
        GREEN,
        BLUE
    }

    public static void main(String[] args) {

        String color = "BLUE";

        // we use valueOf on our enum class
        ColorEnum colorEnum = ColorEnum.valueOf(color);

        System.out.println(colorEnum); // BLUE
    }
}

In java we can simply convert String to enum by using .valueOf() method on our Enum class.

Example:

MyEnumClass.valueOf("MY_ENUM_CONSTANT")

Note:

We need to use the name of enum value exactly like it is defined.

  • letter size must be the same
  • if uppercase letters then we need to use uppercase
  • if lowercase letters then we need to use lowercase
  • no spaces
  • no white spaces

If we don't have exact match then we get exception:

java.lang.IllegalArgumentException: No enum constant ...

1. Documentation - Enum valueOf() method

SyntaxvalueOf(String name)
Called on Enum class eg ColorEnum.valueOf("BLUE")
Parametersname - String, the name of the constant to return
Resultthe enum constant of the specified enum type with the specified name
Example type: ColorEnum
Description

valueOf is static method that takes only one parameter and returns enum constant.
Usage example:
ColorEnum colorEnum = ColorEnum.valueOf("BLUE");

ThrowsIllegalArgumentException – if this enum type has no constant with the specified name (exatch match required - letter size must match, no spaces, no whitespaces)

2. Using Enum value() and String.toUpperCase()

public class Example2 {

    enum ColorEnum {
        RED,
        GREEN,
        BLUE
    }

    public static void main(String[] args) {

        String color = "blue";
        color = color.toUpperCase();

        ColorEnum colorEnum = ColorEnum.valueOf(color);
        System.out.println(colorEnum); // BLUE
    }
}

Output:

BLUE

3. Case with No enum constant

public class Example3 {

    enum ColorEnum {
        RED,
        GREEN,
        BLUE
    }

    public static void main(String[] args) {

        String color = "WHITE";
        ColorEnum colorEnum = ColorEnum.valueOf(color);

        System.out.println(colorEnum);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: 
No enum constant com.dirask.Example3.ColorEnum.WHITE
	at java.lang.Enum.valueOf(Enum.java:238)

Common mistakes

  1. Java enum valueOf uppercase or lowercase - does it matter when I use enum value of?

Merged questions

  1. Java - get enum value from String value
  2. Java - how to convert String to enum object? 
  3. How to cast String to enum value in java?

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey đź‘‹
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more