
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Lookup enum by String value
In this article, we will understand how to lookup enum by string value. An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).
Below is a demonstration of the same −
Suppose our input is −
The string is to lookup is: Java
The desired output would be −
The result is: JAVA
Algorithm
Step 1 - START Step 2 - Declare a string namely input_string, an object of Languages namely result. Step 3 - Define the values. Step 4 - Use the function .valueOf() to fetch the string from the enum function. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we use valueOf() to print the enum values.
public class Demo { public enum Languages { JAVA, SCALA, PYTHON, MYSQL } public static void main(String[] args) { String input_string = "Java"; System.out.println("The string is to lookup is: " +input_string); Languages result = Languages.valueOf(input_string.toUpperCase()); System.out.println("\nThe result is: "); System.out.println(result); } }
Output
The string is to lookup is: Java The result is: JAVA
Example 2
Here, we use .name() function to print the ENUM values..
enum Languages { Java, Scala, Python, Mysql; } public class Demo { public static void main(String[] args) { System.out.println("The values of the ENUM are: "); System.out.println(Languages.Java.name()); System.out.println(Languages.Scala.name()); System.out.println(Languages.Python.name()); System.out.println(Languages.Mysql.name()); } }
Output
The values of the ENUM are: Java Scala Python Mysql
- Related Articles
- Golang Program to Lookup enum by String value
- Java Program to Iterate over enum
- Enum with Customized Value in Java
- C++ program to implement ASCII lookup table
- How to call another enum value in an enum's constructor using java?
- How to convert a String to an enum in Java?
- Java Program to convert an int value to String
- Java Program to Demonstrate the Call By Value
- Java Program to convert string value to float using Float.valueOf()
- How to filter String list by starting value in Java?
- Java Program to parse string date value with default format
- Java Program to convert a Primitive Type Value to a String
- Is it possible to assign a numeric value to an enum in Java?
- Java Program to create a BigDecimal from a string type value
- MySQL ORDER BY 'ENUM' type value based on conditions

Advertisements