42

I have a string value, I also have an array of strings and an enum containing the range also. To get the index of the string in the array, from the value supplied I write this:

Arrays.asList(myClass.BAUD_RATES).indexOf(username)

How do I do this for an enum? Can I use ordinal? Or do i have to make my own method?

The method might go like:

public enum Fruit {
   ...
   static public boolean isMember(String aName) {
       Fruit[] aFruits = Fruit.values();
       for (Fruit aFruit : aFruits)
           if (aFruit.fruitname.equals(aName))
               return aFruit;
       return false;
   }
   ...
}

7 Answers 7

130

Not sure if I understand you correctly but based on question title you may be looking for

YourEnum.valueOf("VALUE").ordinal(); 
//like Directions.valueOf("NORTH").ordinal();
  1. YourEnum.valueOf("VALUE") returns enum value with name "VALUE"
  2. each enum value knows its position (indexed from zero) which we can get by calling ordinal() method on it.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all the answers guys, these all work, apparently that was not my problem. The problem was that if I search the library, for say 9600 in the string array, it works fine. But when I was searching for the same value in the enum it said it was not a value in the enum. 03-15 16:04:19.925: E/AndroidRuntime(19719): Caused by: java.lang.IllegalArgumentException: 19200 is not a constant in slickdevlabs.apps.usb2seriallib.SlickUSB2Serial$BaudRate, so i will just use the string array... No idea why this is the case. Luckily the library gave a string array also.
15

I might not understand you question, but the same code works for enums too:

int index = Arrays.asList(YourEnum.values()).indexOf(YourEnum.ENUM_ITEM);

Or you can get:

int index = YourEnum.valueOf("ENUM_ITEM").ordinal();

Comments

7

Try this simple solution:

Fruit.values()[index]

Comments

2

If you want to retrieve the index, you can use ordinal. If you want to assign some specific value to String, you may define your own method to retrieve it.

 enum DAY
 {
  MONDAY(10),
  TUESDAY(20);
  int value;
  DAY(int x)
  {
   this.value = x;
  }
  public int getValue()
  {
    return value;
   }

Now value and ordinal can be retrieved as :

    for(DAY s : DAY.values() )
    {
        System.out.println(s.ordinal());
        System.out.println(s.getValue());
    }

Comments

1

It's really simple

TestEnum test = TestEnum.c;
log(test.index.toString());

enum TestEnum{
  c,d
}

Comments

0

The following logic will also work.

If you want to check and you know the fruitname already don't use for loop go with the approach mentioned by Pshemo

for (Fruit aFruit : aFruits)
           if (aFruit.name().equals(aName))
               return aFruit.ordinal();

Comments

0

Adding here the solution that worked for me:

YourEnum.values.indexOf("VALUE");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.