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 6737907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:18:13+00:00 2026-05-26T11:18:13+00:00

I’m wondering about a good method to enumerate in Java. I’ve wondered this for

  • 0

I’m wondering about a good method to enumerate in Java.

I’ve wondered this for a long time, and typically I come up with something that includes several independent enums and functions, and some defines and such.

But basically, I want to define unique number keys for various types of enumerations, with sub enumerations.

For example, I’m trying to implement a language with various keywords and symbols, with a number corresponding to that element, in the parenthesis. Like element(id).

Keywords: program(1), call(2), if(3), else(4), elsif(5), …
Symbols ‘;'(6), ‘,'(7), ‘='(8), ‘+'(9), …
Operations: and(10), or(11), …

What would be the best way to accomplish this?

I hope my posting was clear. But basically I’m trying to create categories of element types, and then the element definitions within them, that have an associated numeric value.

The purpose of this would be so that I could, check an input string to return the integer value if it identified substrings in the input as elements in the “dictionary” above.

Thanks in advance!

  • 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-26T11:18:14+00:00Added an answer on May 26, 2026 at 11:18 am

    You shouldn’t care about the numeric values of the enums themselves and you shouldn’t depend on them. The reason being that, if in the future you or someone else decides to add a new enum to the middle of your list of enums, all your numeric values are now invalid and you’ll have to go through the code and change them. Hence it’s better to depend on the actual enum rather than the enum’s numeric value.

    If you’re trying to “group” enums together (if I understand your question correctly), you could use something like a marker interface. So for example you could have:

    public interface Operators {
    }
    
    public enum BooleanOperators implements Operators {    
       AND, OR, NOT, XOR
    }
    
    public enum ArithmeticOperators implements Operators {
       ADD, SUBTRACT, MULTIPLY, DIVIDE
    }
    

    Then instead of having two methods, one that would accept enums of type BooleanOperators and another that would accept types of ArithmeticOperators, you can have a single method that accepts type Operators:

    public void doSomethingWithOperators(Operators operators) {
        ....
    }
    

    If you want to tie explicit values to your enums, there is a safe way to do it:

    public interface Operators {
        int getCode();
    }
    
    public enum BooleanOperators implements Operators {
    
       private int code;
    
       AND(1), OR(2), NOT(3), XOR(4)
    
       private BooleanOperators(int code) {
           this.code = code;
       }
    
       public int getCode() {
           return this.code;
       }
    }
    

    Then you can do ArithmeticOperators.ADD.getCode(), which will return the code associated with that enum. This method is safer because you can see the value being explicitly associated with the enum versus being implicitly associated based on their order of definition. So when you, or someone else adds a new enum, they are expected to associate a (hopefully unique) integer value with the new enum.

    NOTE: The marker interface is not strictly necessary in this case; it just enforces the requirement that all enums that implement the interface should have the getCode() method. The following is just as valid:

    public enum BooleanOperators {
    
       private int code;
    
       AND(1), OR(2), NOT(3), XOR(4)
    
       private BooleanOperators(int code) {
           this.code = code;
       }
    
       public int getCode() {
           return this.code;
       }
    }
    

    I assume this is what you want. If not, please let me know!

    EDIT

    Based on your comment, I would use a marker interface (let’s call it Token) that is applied to all your enums, and then use a map of type Map<String, Token>. You will initialize this map with your tokens and the corresponding enum. When your parser returns tokens, you can look up the enum using your map.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT

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.