Avatar ·

How to get enum value from string in Java?

Hello everyone! I'm new to Java and encountered a problem when working with enums. I have an enum that describes order statuses:\n\n```java\npublic enum OrderStatus {\n NEW,\n PROCESSING,\n SHIPPED,\n DELIVERED,\n CANCELLED\n}\n```\n\nI receive a string from the user via console or from a database, for example "PROCESSING" or "processing". I need to convert this string to the corresponding enum value to use it in the application logic.\n\nThe problem is that I don't know how to do this correctly. I tried simply assigning the string to a variable of type OrderStatus, but the compiler gives an error. I've heard that there is a valueOf() method, but it throws an exception if the string doesn't match the constant name. What if the string is written in a different case? Or with