Servant Of God
Servant Of God

Reputation: 1

How to convert an enum from another enum into a string

I have a pair of related enums in Java.

Enum Alphabet

{
  A, B, C;
}

Enum Phonetic

{
  Alpha (Alphabet.A, Amber)
  Bravo (Alphabet.B, Beta)
  Charlie (Alphabet.C, Cookie)
}
  

How do I convert the Alphabet.A values in enum phonetic into a string?

Upvotes: 0

Views: 46

Answers (1)

phatfingers
phatfingers

Reputation: 10250

If you have two sets of enums that have equivalent values stored in the same order, then you can use the ordinal value from one to look up the equivalent value from the other.

int ord=Alphabet.A.ordinal();
Phonetic equiv=Phonetic.values()[ord];
System.out.println(equiv);

Upvotes: 1

Related Questions