How to convert a string to an enum value in Java

You can use the valueOf() method to convert a string to an enum value in Java. The valueOf() method takes a string as an argument and returns the corresponding enum object.

Let us say we have the following enum class that represents the days of the week:

public enum Day {

The following example demonstrates how you can convert a string to a Day object using the valueOf() method:

String str = "FRIDAY";

Day enumDay = Day.valueOf(str);
System.out.println(enumDay); // FRIDAY

Note that the valueOf() method is case-sensitive. The string value must be an exact match of an enum constant, including the case. Otherwise, an IllegalArgumentException exception is thrown.

The following two examples will fail due to an invalid string or case mismatch:

// ❌ Fail due to case mismatch
Day mismatchStr = Day.valueOf("Monday");

// ❌ Fail due to invalid value
Day invalidStr = Day.valueOf("Weekend");

// ✅ Correct value
Day correctStr = Day.valueOf("MONDAY");

To handle case mismatch, you can transform the given string to uppercase, as shown below:

String str = "Monday";

// Transform the string to uppercase
Day enumDay = Day.valueOf(str.toUpperCase());
System.out.println(enumDay); // MONDAY

Alternatively, you can also use the Streams API to convert a string to an enum value:

String str = "Monday";

Optional<Day> enumDay =
        .filter(d ->

if (enumDay.isPresent()) {
    System.out.println("✅ Enum exists: " + enumDay.get());
} else {
    System.out.println("❌ Enum does not exist!");

// ✅ Enum exists: MONDAY

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.