Java enum

This is a discussion on Java enum within the Tech Board forums, part of the Community Boards category; I am having problems with trying to use Enums in a switch/case in java. My enum class: Code: public enum ...

  1. #1
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

    Java enum

    I am having problems with trying to use Enums in a switch/case in java.

    My enum class:
    Code:
    public enum CharClass {
    	Amazon,
    	Sorceress,
    	Necromancer,
    	Paladin,
    	Barbarian,
    	Druid,
    	Assassin
    }
    My switch/case I would like to use my enums with.
    Code:
    		int ClassName = in.read();
    		switch(ClassName) {
    			case CharClass.Amazon: 		System.out.println("Class: Amazon");	 break;
    			case CharClass.Sorceress:	System.out.println("Class: Sorceress");  break;
    			case CharClass.Necromancer:	System.out.println("Class: Necromancer");break;
    			case CharClass.Paladin: 	System.out.println("Class: Paladin");	 break;
    			case CharClass.Barbarian: 	System.out.println("Class: Barbarian");  break;
    			case CharClass.Druid: 		System.out.println("Class: Druid"); 	 break;
    			case CharClass.Assassin: 	System.out.println("Class: Assassin");	 break;
    		}
    I posted on a Java board, but it does not seem to generate the traffic like here. (Yes this is for D2X...)

    Edit: Noticed I didn't put the specific problem in here. For each case I am getting
    Code:
    [javac] C:\DATA\development\java\D2SReader\source\D2SReader.java:36: incompatible types
    [javac] found   : CharClass
    [javac] required: int
    [javac]                     case CharClass.Amazon:          System.out.println("Class: Amazon");     break;
    [javac]                                       ^
    Last edited by Wraithan; 06-19-2006 at 10:15 PM.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    You can switch on enumeration constants by defining an appropriate method in the enumeration class.

    see this: http://java.sun.com/j2se/1.5.0/docs/...age/enums.html

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I found a way that suited the way I wanted to do it:

    Code:
    		int ClassName = in.read();
    		switch(CharClass.values()[ClassName]) {
    			case Amazon: 	 System.out.println("Class: Amazon");	  break;
    			case Sorceress:	 System.out.println("Class: Sorceress");  break;
    			case Necromancer:System.out.println("Class: Necromancer");break;
    			case Paladin: 	 System.out.println("Class: Paladin");	  break;
    			case Barbarian:  System.out.println("Class: Barbarian");  break;
    			case Druid: 	 System.out.println("Class: Druid"); 	  break;
    			case Assassin: 	 System.out.println("Class: Assassin");	  break;
    		}
    Thank you though!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mats, the java answers
    By Jaqui in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 04-22-2008, 02:12 AM
  2. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  3. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  4. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM
  5. C or Java as a first language
    By CorJava in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 10-23-2002, 05:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21