-4

How to retrieve the enum name using value. By passing JOHN I need to retrieve the value as single

public enum Status {
    JOHN("single"),
    ALEX("married"),
    MARTHA("not known");
}

Is it possible to have default value as well in-case value does not match?

firstpostcommenter
  • 2,328
  • 4
  • 30
  • 59

3 Answers3

4

To do this you need to define a constructor and a String variable. Then you could create a getter method to return the String variable:

public enum Status {
    JOHN("single"),
    ALEX("married"),
    MARTHA("not known");
    private String value;
    private Status(String str) {
       value = str;
    }
    public String getValue() {
        return this.value;
    }
}

And then to get the value you can do:

Status.JOHN.getValue()

To get the enum from a String you can use the valueOf() method:

Status.valueOf("JOHN").getValue();

However this will throw an error if the inputted String does not correspond to an enum. You could either wrap it in a try-catch to assign a default to it:

try {
   Status.valueOf("JOHN").getValue();
} catch(IllegalArgumentException e) {
   //Assign default
}

However a better design might be to put the possibilities into a HashMap and see if the inputted String is in the HashMap:

Map<String, Status> status = new HashMap<>();
status.put("JOHN", Y.JOHN);
if(status.containsKey(input)) {
   //Do stuff
} else {
  //Assign default 
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
3

Define your enum like

enum Status {
    JOHN("single"), ALEX("married"), MARTHA("not known");

    private String value;

    private Status(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

}

Now to fetch the value :

System.out.println(Status.JOHN.getValue());

Here define a parameterized constructor for each enum and store this value in a member variable.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
1

A Java enum is an object, just like other objects in most ways. You can give it fields, methods, and constructors.

In this case, you can provide a marriage field and set it in a constructor just like you would for any other object.

Check out the official page on Java enums to see official examples, specifically the example with planets for your case. The Java Tutorials: Enum Types


Note also that you might not want to represent this specific data as an enum. You will be limited to only those 3 specific people by doing so. This is only appropriate if you know that you definitely will have only this specific list of people for the entire lifetime of this code. Otherwise you should just use a normal object.

A better idea might be to have a marriage enum and give your people a field of that type.

public enum MarriageStatus
{
    Married,
    NotMarried,
    Unknown;
}

public class Person
{
    String name = "Unknown";
    MarriageStatus marriageStatus = MarriageStatus.Unknown;
}
Loduwijk
  • 1,950
  • 1
  • 16
  • 28