How to Compare String to Enum Java

How to Compare String to Enum Java? The Enum is a keyword used while defining enumeration which contains constant values which are accessible. Enum expands as enumeration. The string is a collection of a sequence of characters that are represented between double quotes (“ ”). Now in this blog, we are comparing the string to enum.

For example:-
1. Enum: {ONE, TWO, THREE, FOUR};
String: “TWO”
Output = equal
2. Enum: {ONE, TWO, THREE, FOUR};
String: “NINE”
Output = not equal.

Program to Compare String to Enum Java

Here we are comparing the enum of days to string, this is obviously not equal hence the result will be not equal. The enum is a different type and the string is different while comparing it is not equal. Check the below program.

enum Day {
   SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
   THURSDAY, FRIDAY, SATURDAY
}

public class Main {
   public static void main(String[] args) {
      Main obj = new Main();
      obj.checkEnum("TUESDAY");
   }

   private void checkEnum(String string) {
      Day[] Days = Day.values();
      for (Day day : Days) {
         System.out.println("Day: " + day.name());
         if (day.equals(string)) {
            System.out.println("Equal");
         } else {
            System.out.println("Not equal");
         }
      }
   }
}

Output:-

Day: SUNDAY
Not equal
Day: MONDAY
Not equal
Day: TUESDAY
Not equal
Day: WEDNESDAY
Not equal
Day: THURSDAY
Not equal
Day: FRIDAY
Not equal
Day: SATURDAY
Not equal

How To Compare Enum With String In Java

As we saw in the above program the string and enum cannot be compared directly even if it looks the same as the types are different which might result in inequality, so to overcome this we need to first convert the enum value to string and then compare to do this here we are using toString() method.

enum Day {
   SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
   THURSDAY, FRIDAY, SATURDAY
}

public class Main {
   public static void main(String[] args) {
      Main obj = new Main();
      obj.checkEnum("TUESDAY");
   }

   private void checkEnum(String string) {
      Day[] Days = Day.values();
      for (Day day : Days) {
         System.out.println("Day: " + day.name());
         if (day.toString().equals(string)) {
            System.out.println("Equal");
            break;
         } else {
            System.out.println("Not equal");
         }
      }
   }
}

Output:-

Day: SUNDAY
Not equal
Day: MONDAY
Not equal
Day: TUESDAY
Equal

Now let us see the same thing with the name() method. The name() method yields the same output as the toString() method but the difference is the name() method returns the enum constant that is exactly as same as the one in the declaration. The toString() method returns which are contained in the declaration.

enum Day {
   SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
   THURSDAY, FRIDAY, SATURDAY
}

public class Main {
   public static void main(String[] args) {
      Main obj = new Main();
      obj.checkEnum("TUESDAY");
   }

   private void checkEnum(String string) {
      Day[] Days = Day.values();
      for (Day day : Days) {
         System.out.println("Day: " + day.name());
         if (day.name().equals(string)) {
            System.out.println("Equal");
            break;
         } else {
            System.out.println("Not equal");
         }
      }
   }
}

Output:-

Day: SUNDAY
Not equal
Day: MONDAY
Not equal
Day: TUESDAY
Equal

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.