?
Solved

Need Java Enumeration + Array Help

Posted on 2011-10-05
8
Medium Priority
?
352 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:themrrobert
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36920720
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36920780
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

0
 
LVL 13

Author Comment

by:themrrobert
ID: 36920787
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.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36920792
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

0
 
LVL 13

Author Comment

by:themrrobert
ID: 36920833
@ 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 )
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36920899
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

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36920919
And you can define inside or outside the class
But you need to use ordinal() to get int values from enum
public class MyObj  {


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

    }


}
  enum CatTypes {
 CAT_TABBY, CAT_CALICO, CAT_ZOMBIE
}

Open in new window


Output:
0
1

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36920946
>>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
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question