Avatar of themrrobert
themrrobertFlag for United States of America

asked on 

Need Java Enumeration + Array Help

I am making a class, here is what i need:

Items can have the same value, for example:

<code>
public class MyObj extends DynamicGameObject {

public enum CatTypes {
 CAT_TABBY, CAT_CALICO, CAT_ZOMBIE
}

public static final int NUM_LIVES[CAT_TABBY] = 9;
public static final int NUM_LIVES[CAT_CALICO] = 9;
public static final int NUM_LIVES[CAT_ZOMBIE] = 1;

}
</code>

Now I already tried defining them as normal public int's instead of static final, and it still gives same error:
Syntax error on token CAT_ZOMBIE, delete Token.

How can I make this work? I need to be able to reference number of lives based on this enumerated type
Java

Avatar of undefined
Last Comment
CEHJ
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You're kind of defeating the object of enum (by using int constants). What you should do is initialize the enum class with the int value. See the 'Planet' enum here (you would use int instead of doubles)

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
Avatar of for_yan
for_yan
Flag of United States of America image

This is how you do it:

public class MyObj  {

   public static  int[] NUM_LIVES;

    public static void main(String[] args) {

 NUM_LIVES[CatTypes.CAT_TABBY.ordinal()] = 9;

    }


}

 enum CatTypes {
 CAT_TABBY, CAT_CALICO, CAT_ZOMBIE
}

Open in new window

Avatar of themrrobert
themrrobert
Flag of United States of America image

ASKER

I already have that page open, and the 2nd example is too confusing. The enum encapsulates the entire class, including the main(). This is not acceptable, and its not easily translatable to my needs.

Avatar of for_yan
for_yan
Flag of United States of America image

added more cats:

public class MyObj  {

   public static  int[] NUM_LIVES;

    public static void main(String[] args) {

 NUM_LIVES[CatTypes.CAT_TABBY.ordinal()] = 9;
  NUM_LIVES[CatTypes.CAT_CALICO.ordinal()] = 9;
   NUM_LIVES[CatTypes.CAT_ZOMBIE.ordinal()] = 9;      

    }


}

 enum CatTypes {
 CAT_TABBY, CAT_CALICO, CAT_ZOMBIE
}

Open in new window

Avatar of themrrobert
themrrobert
Flag of United States of America image

ASKER

@ CEHJ: I'd like to use this, but simpler.

How do I have it return a single int?

I defined the values, and made a constructure int NumOfLives, but how do i tell it to return the unnamed values ( X )
Avatar of for_yan
for_yan
Flag of United States of America image

This works without problems - I tested it - see output below

public class MyObj  {

   static enum CatTypes {
 CAT_TABBY, CAT_CALICO, CAT_ZOMBIE
}
   public static  int[] NUM_LIVES;

    public static void main(String[] args) {

        NUM_LIVES = new int[5];
 NUM_LIVES[CatTypes.CAT_TABBY.ordinal()] = 9;
  NUM_LIVES[CatTypes.CAT_CALICO.ordinal()] = 9;
   NUM_LIVES[CatTypes.CAT_ZOMBIE.ordinal()] = 9;
        System.out.println(CatTypes.CAT_TABBY.ordinal());
          System.out.println(CatTypes.CAT_CALICO.ordinal());

    }


}

Open in new window


Output:
0
1

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>I need to be able to reference number of lives based on this enumerated type

As i mentioned, there's no need to do what you've just been led to believe is the solution: it's completely unnecessary and defeats the object of the enum
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo