Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the proper way to compare a String to an enum value?

Tags:

java

enums

Homework: Rock Paper Scissors game.

I've created an enumeration:

      enum Gesture{ROCK,PAPER,SCISSORS}; 

from which I want to compare values to decide who wins--computer or human. Setting the values works just fine, and the comparisons work properly (paper covers rock, rock crushes scissors, scissors cuts paper). However, I cannot get my tie to work. The user is declared as the winner any time there's a tie.

Ahhh...crap...this will clarify: userPick is a String with values rock, paper, or scissors. I'm unable to use == to compare userPick to computerPick, which, as you can see below, is cast as type Gesture from my enum.

      if(computer == 1)          computerPick = Gesture.ROCK;       else          if(computer == 2)            computerPick = Gesture.PAPER;          else            computerPick = Gesture.SCISSORS;       if(userPick.equals(computerPick))        {           msg = "tie";           ++tieGames;        }            etc.... 

I am guessing that there's an issue with rock not being equal to ROCK, or the String userPick not being able to match Gesture computerPick because the latter isn't a String. However, I'm not able to find an example of a similar circumstance in my textbook or Oracle's Java Tutorials, so I'm not sure how to correct the problem...

Any hints?

like image 450
dwwilson66 Avatar asked Mar 25 '12 05:03

dwwilson66


People also ask

Can == be used on enum?

equals method uses == operator internally to check if two enum are equal. This means, You can compare Enum using both == and equals method.

Can we compare enum with string in C?

In C you'd have to write a function for that. It would essentially be a switch statement. If colString doesn't match any of the enum elements it won't match numbersAndColours . There is no need to compare it against all of the elements.


2 Answers

I'm gathering from your question that userPick is a String value. You can compare it like this:

if (userPick.equalsIgnoreCase(computerPick.name())) . . . 

As an aside, if you are guaranteed that computer is always one of the values 1, 2, or 3 (and nothing else), you can convert it to a Gesture enum with:

Gesture computerPick = Gesture.values()[computer - 1]; 
like image 111
Ted Hopp Avatar answered Sep 26 '22 00:09

Ted Hopp


You should declare toString() and valueOf() method in enum.

 import java.io.Serializable;  public enum Gesture implements Serializable {     ROCK,PAPER,SCISSORS;      public String toString(){         switch(this){         case ROCK :             return "Rock";         case PAPER :             return "Paper";         case SCISSORS :             return "Scissors";         }         return null;     }      public static Gesture valueOf(Class<Gesture> enumType, String value){         if(value.equalsIgnoreCase(ROCK.toString()))             return Gesture.ROCK;         else if(value.equalsIgnoreCase(PAPER.toString()))             return Gesture.PAPER;         else if(value.equalsIgnoreCase(SCISSORS.toString()))             return Gesture.SCISSORS;         else             return null;     } } 
like image 33
kandarp Avatar answered Sep 25 '22 00:09

kandarp