Java Program to Convert the String Into an Enum Constant

You are currently viewing Java Program to Convert the String Into an Enum Constant

Java program to convert the string into an enum constant

// Java program to convert the string 
// into an enum constant

public class Main {
  enum Vehicle {
    BIKE,
    CAR,
    BUS
  }

  public static void main(String[] args) {
    String str = "BUS";

    Vehicle v = Vehicle.valueOf(str);

    System.out.println(str);
  }
}

Output

BUS

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply