How do I store this data in a Java enum?

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 am 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 it doesn't work. I am considering storing a text value that summarizes the parameter that the user sees on this web page.

+3


source to share


7 replies


Have a look at the enum tutorial , specifically an example Planet

. You can do the same for example.



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;
  }
}

      

+8


source


Something like this might 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;
    }
}

      

+1


source


You cannot assign strings to enumerate values ​​in Java the way you are trying.

The way to do it:

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());
      }
}

      

+1


source


You cannot have spaces in member names and you cannot assign enum values, these are objects, not constants.

0


source


The enumeration name must be an identifier (e.g. one word, not a string)

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

      

You can bind string values ​​to them if you like (although you can get a default value equal to the name of the identifier, use the method name()

) by binding the String element to the enumeration 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 this related string, you also need to provide a public accessor.

0


source


    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;
     }

   }

      

0


source


Java enums are not built this way. Check

The Java Tutorials: Enumeration 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;
  }
}

      

0


source







All Articles