ENUM returning only key and not value

Asked

Viewed 985 times

0

I am trying in various ways to get the value of an ENUM, but it just returns its "key". Below follows the ENUM:

public enum SaleType {
    BOUGHT("Comprou"),
    SEND_BUDGET("Enviar Orçamento"),
    SUBMITTED_BUDGET("Orçamento Enviado"),
    NOT_WANTED("Não quis"),
    MAYBE_FUTURE("Pode Querer no Futuro");

    private final String sale ;

    private SaleType(String sale){
        this.sale=sale;
    }

    public String getSaleType(){
        return sale;
    }

}

For example, I want to get the "content" of SEND_BUDGET, then made a loop in which will take all the values of my ENUM and make an IF to compare if the value matches...

 for (SaleType s : SaleType.values()) {
    if (si.getSaleType().equals(s)) {
        String name = si.getSaleType();
        SaleType valueOf = s.valueOf(SaleType.class, name);
        System.out.println(valueOf);
    }
}

But the problem is that it will always return me the key Ex:. SEND_BUDGET and not its corresponding value "Send Budget".

It is probably a basic doubt, but I have tried some forms and without success

  • 1

    By chance si is the type SaleType? If so, enums are one of the few cases where it is safe to compare with == (i.e. change your own if for si == s).

  • Yes @mgibsonbr my si is of the type Saletype. Interesting! I will put in practice.

1 answer

1


Use as follows:

SaleType.SEND_BUDGET.getSaleType()
  • Exactly! Lack of attention to mine.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.