Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4620966
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:37:58+00:00 2026-05-22T02:37:58+00:00

I have an java Enum class, and at runtime I will read in a

  • 0

I have an java Enum class, and at runtime I will read in a value from command line, and I want to correspond this value to a value in my Enum class. Shipper is my Enum Class. Is there a better way do this this, instead of if and else if below? This look ugly.

private List<Shipper> shipperName = new ArrayList<Shipper>();
...
public void init(String s){ 
    if(s.equals("a")){
        shipperName.add(Shipper.A);
    }else if(s.equals("b")){
        shipperName.add(Shipper.B);
    }else if(s.equals("c")){
        shipperName.add(Shipper.C);
    }else if(s.equals("d")){
        shipperName.add(Shipper.D);
    }else if(s.equals("e")){
        shipperName.add(Shipper.E);
    }else{
        System.out.println("Error");
    }
}

Here is my Shipper.class

public enum Shipper
{
    A("a"),
    B("b"),
    C("c"),
    D("e"),
    F("f")
;

    private String directoryName;

    private Shipper(String directoryName)
    {
         this.directoryName = directoryName;
    }

    public String getDirectoryName()
    {
         return directoryName;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:37:59+00:00Added an answer on May 22, 2026 at 2:37 am

    Yes add the string into the enum’s constructor (specify a constructor in the enum which takes a String) and create the enums with a text value of a, b , c. Etc. Then implement a static factory method on the enum which takes a string and returns the enum instance. I call this method textValueOf. The existing valueOf method in enum cannot be used for this. Something like this:

    public enum EnumWithValueOf {
    
        VALUE_1("A"), VALUE_2("B"), VALUE_3("C");
    
        private  String textValue;
    
        EnumWithValueOf(String textValue) {
            this.textValue = textValue;
        }
    
        public static EnumWithValueOf textValueOf(String textValue){
    
            for(EnumWithValueOf value : values()) {
                if(value.textValue.equals(textValue)) {
                    return value;
                }
            }
    
            throw new IllegalArgumentException("No EnumWithValueOf
                                for value: " + textValue);  
        }   
    }
    

    This is case insensitive and the text value can be different to the enum name – perfect if your database codes are esoteric or non descriptive, but you want better names in the Java code. Client code then do:

    EnumWithValueOf enumRepresentation = EnumWithValueOf.textValueOf("a");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming you have a hypothetical enum in java like this (purely for demonstration purposes,
An enum in Java implements the Comparable interface. It would have been nice to
I have a really strange enum bug in Java. for(Answer ans : assessmentResult.getAnswersAsList()) {
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
I want to create an enum-like set of strings in a way that will
I have java class which has a variable of type char now I have
I have Java and Flash client applications. What is the best way for the
Here is what I have: JAVA_HOME=C:\Software\Java\jdk1.5.0_12 (points to JDK 5.0) In Eclipse Installed Runtimes
Does Java have a built-in way to escape arbitrary text so that it can
Does Java have a data type that represents a period of time eg 34

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.