Languages
[Edit]
EN

Java - convert String to Enum object

6 points
Created by:
Root-ssh
175400

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?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - String conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join