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

  • Home
  • SEARCH
  • 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 272185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:16:36+00:00 2026-05-12T00:16:36+00:00

I’m very familiar with using Enums in other languages, but I’m having some difficulty

  • 0

I’m very familiar with using Enums in other languages, but I’m having some difficulty in Java with a particular use.

The Sun documentation for Enums boldly states:

“Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers.”

Well, that’s dandy, but I kind of need to have a constant datatype representation for each of the Enums, for comparison reasons in a switch statement. The situation is as follows: I’m constructing nodes which will represent a given space, or ‘slot’ in a maze graph, and these nodes must be able to be constructed from a 2D integer array which represents the maze. Here’s what I’ve got for the MazeNode class, which is currently where the problem is (the switch statement barks):

NOTE: I know this code does not function, due to the dynamic item in the case statement. It is there to illustrate what I’m after.

public class MazeNode
{
    public enum SlotValue
    {
        empty(0),
        start(1),
        wall(2),
        visited(3),
        end(9);

        private int m_representation;

        SlotValue(int representation)
        {
            m_representation = representation;
        }

        public int getRepresentation()
        {
            return m_representation;
        }
    }

    private SlotValue m_mazeNodeSlotValue;

    public MazeNode(SlotValue s)
    {
        m_mazeNodeSlotValue = s;
    }

    public MazeNode(int s)
    {

        switch(s)
        {
            case SlotValue.empty.getRepresentation():
                m_mazeNodeSlotValue = SlotValue.start;
                break;
            case SlotValue.end.getRepresentation():
                m_mazeNodeSlotValue = SlotValue.end;
                break;

        }
    }

    public SlotValue getSlotValue()
    {
        return m_mazeNodeSlotValue;
    }

}

So the code complains on the switch statement with “case expressions must be constant expressions” — I can see why the compiler might have trouble, since technically they are dynamic, but I’m not sure what approach to take to resolve this. Is there a better way?

The bottom line is I need the Enum to have corresponding integer values for comparison against the incoming 2D array of integers in the program.

  • 1 1 Answer
  • 1 View
  • 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-12T00:16:37+00:00Added an answer on May 12, 2026 at 12:16 am

    You can use something like this:

    import java.util.HashMap;
    import java.util.Map;
    
    public class MazeNode {
    
        public enum SlotValue {
            empty(0), start(1), wall(2), visited(3), end(9);
    
            protected int m_representation;
    
            SlotValue(int representation) {
                m_representation = representation;
    
            }
    
            private static final Map<Integer, SlotValue> mapping = new HashMap<Integer, SlotValue>();
    
            static {
                for (SlotValue slotValue : SlotValue.values()) {
                    mapping.put(slotValue.m_representation, slotValue);
                }
            }
    
            public static SlotValue fromRepresentation(int representation) {
                SlotValue slotValue = SlotValue.mapping.get(representation);
                if (slotValue == null)
                    // throw your own exception
                    throw new RuntimeException("Invalid representation:" + representation);
                return slotValue;
            }
        }
    
        private SlotValue m_mazeNodeSlotValue;
    
        public MazeNode(SlotValue s) {
            m_mazeNodeSlotValue = s;
        }
    
        public MazeNode(int s) {
            m_mazeNodeSlotValue = SlotValue.fromRepresentation(s);
    
        }
    
        public SlotValue getSlotValue() {
            return m_mazeNodeSlotValue;
        }
    
        public static void main(String[] args) {
            MazeNode m = new MazeNode(2);
            System.out.println(m.getSlotValue());
            m = new MazeNode(9);
            System.out.println(m.getSlotValue());
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.