6

What's the best way to store this data in a Java enum?

<select>
    <option></option>
    <option>Recommend eDelivery</option>
    <option>Require eDelivery</option>
    <option>Require eDelivery unless justification provided</option>
</select>

I'm new to java and have tried things like

public enum Paperless { 
      "None" = null,
      "Recommend eDelivery" = "Recommend eDelivery",
      "Require eDelivery" = "Require eDelivery",
      "Require eDelivery unless justification provided" = "Require eDelivery w/out justification"
}

But this doesn't work. I'm considering the possibility of storing a text value that summarizes the option that the user sees on this web page.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Ben
  • 60,438
  • 111
  • 314
  • 488

7 Answers7

11

Take a look at the enum tutorial, more specifically the Planet example. You can do the same, e.g.

public enum Paperless{
  NONE( null ),
  RECOMMENDED_DELIVERY( "Recommended delivery" ),
  ...//put here the other values
  REQUIRED_DELIVERY( "Required delivery" );
  private String name;
  Paperless( String name ){
    this.name = name;
  }
  public String getName(){
    return this.name;
  }
}
Robin
  • 36,233
  • 5
  • 47
  • 99
  • 2
    Just a small point that easily trips up Java novices: there needs to be a semicolon separating the enum names from the body (i.e., just before `private String name;` in this example). – Ted Hopp Mar 28 '12 at 20:22
  • @TedHopp completely correct. Due to my ... the ; got left out. Will update the snippet. Thanks for noticing – Robin Mar 28 '12 at 20:23
  • If I had that enum as part of another class, say a model. How would I access the Paperless object from outside the current class? – Ben Mar 29 '12 at 12:19
  • @Webnet like you would access any other class: ModelClass.Paperless.NONE for example – Robin Mar 29 '12 at 12:37
1

Something like this can work for your case:

public enum PaperLess {
    NONE("none"),
    RECOMMEND("Recommend eDelivery"),
    REQUIRE("Require eDelivery"),
    REQUIRE_JUSTIFIED("Require eDelivery unless justification provided");

    private String value;

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

    public String getValue() {
       return value;
    }
}
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

You can't assign strings to enum values in Java in the way that you are trying.

The way to do it would be:

public enum Paperless { 
      None(null), 
      RecommendedDelivery("Recommended Delivery"), 
      RequireEDelivery("Require eDelivery"), 
      RequireEDeliveryUnlessJustification("Require eDelivery unless justification provided");

      private final String value;   

      Paperless(String value) {
        this.value = value;
      }

      private String enumValue() { return value; }

      public static void main(String[] args) {
        for (Paperless p : Paperless.values())
           System.out.println("Enum:" + p + "; Value:" + p.enumValue());
      }
}
jhenderson2099
  • 956
  • 8
  • 17
0

You can't have spaces in the names of members and you can't assign enum values, they are objects, not constants.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

The name of the enum must be an identifier (e.g. one-word, not a string)

public enum Paperless {
  None,
  RecommendEDelivery,
  ...
}

You can associate string values with them if you want (although you can get the default too that equals to the identifier name, usign the name() method) by associating a String member with the enum type and providing a custom constructor.

public enum Paperless {
  None("None"),
  RecommendEDelivery("Recommend eDelivery"),
  ...;

  private String myValue;

  private Paperless(String name) {myValue=name;)
}

To access that associated string, you need to provide a public accessor method as well.

Attila
  • 28,265
  • 3
  • 46
  • 55
0
    public enum Paperless {
        NONE("None"),
        RECOMMEND("Recommend eDelivery"),
        REQUIRE("Require eDelivery"),
        REQUIRE_UNLESS("Require eDelivery unless justification provided"),;

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

     public String getValue(){
         return this.value;
     }

   }
A Null Pointer
  • 2,261
  • 3
  • 26
  • 28
0

Java enums aren't constructed in that way. Check out

Java Tutorials: Enum Types

Java - Convert String to enum: #2

Yours might look something like this:

public enum Paperless {
  NONE(""),
  RECOMMEND("Recommend eDelivery"),
  REQUIRE("Require eDelivery"),
  REQUIRE_UNLESS("Require eDelivery unless justification provided");

  private String text;

  Paperless(String text) {
    this.text = text;
  }

  public String getText() {
    return this.text;
  }
}
Community
  • 1
  • 1
GetSet
  • 534
  • 2
  • 6