Getting String value from enum in Java

JavaEnums

Java Problem Overview


I have a enum defined like this and I would like to be able to obtain the strings for the individual statuses. How should I write such a method?

I can get the int values of the statuses but would like the option of getting the string values from the ints as well.

public enum Status {
    PAUSE(0),
    START(1),
    STOP(2);

    private final int value;

    private Status(int value) {
        this.value = value
    }

    public int getValue() {
        return value;
    }
}

Java Solutions


Solution 1 - Java

if status is of type Status enum, status.name() will give you its defined name.

Solution 2 - Java

You can use values() method:

For instance Status.values()[0] will return PAUSE in your case, if you print it, toString() will be called and "PAUSE" will be printed.

Solution 3 - Java

Use default method name() as given bellows

public enum Category {
        ONE("one"),
        TWO ("two"),
        THREE("three");

        private final String name;

        Category(String s) {
            name = s;
        }

    }

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Category.ONE.name());
    }
}

Solution 4 - Java

You can add this method to your Status enum:

 public static String getStringValueFromInt(int i) {
     for (Status status : Status.values()) {
	     if (status.getValue() == i) {
	         return status.toString();
	     }
	 }
     // throw an IllegalArgumentException or return null
	 throw new IllegalArgumentException("the given number doesn't match any Status.");
 }

public static void main(String[] args) {
	System.out.println(Status.getStringValueFromInt(1)); // OUTPUT: START
}

Solution 5 - Java

I believe enum have a .name() in its API, pretty simple to use like this example:

private int security;
public String security(){ return Security.values()[security].name(); }
public void setSecurity(int security){ this.security = security; }

    private enum Security {
            low,
            high
    }

With this you can simply call

yourObject.security() 

and it returns high/low as String, in this example

Solution 6 - Java

You can use custom values() method:

public enum SortType { Scored, Lasted;

     public int value(){
         return this == Lasted ? 1:0;
     }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMozbiView Question on Stackoverflow
Solution 1 - JavaharshView Answer on Stackoverflow
Solution 2 - JavaJuvanisView Answer on Stackoverflow
Solution 3 - JavaBiswajit KarmakarView Answer on Stackoverflow
Solution 4 - JavaM. AbbasView Answer on Stackoverflow
Solution 5 - JavaJ.StangeView Answer on Stackoverflow
Solution 6 - JavaPaxLightningView Answer on Stackoverflow