Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enum  RSS feed

 
Tom Orr
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me why this does not work and how to fix it

private enum GuessedValue{A,2,3,4,5,6,7,8,9,J,Q,K}

I think it sees it as both integers and chars
 
Paul Clapham
Sheriff
Posts: 22530
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing -- in the absence of any error messages (surely you must have got something from the compiler?) -- that "2" isn't a valid identifier.
 
Deep Purohit
Greenhorn
Posts: 9
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul said it right. Along with this you can not have a private enum, it can be only public.
 
Tom Orr
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes the compiler tells me an identifier is expected for the integers. I changed it to public and still get the same message.

Im going to try going another direction with this.

my enum will be public and contain {ACE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,KING}

 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read about identifiers here.
 
Tom Orr
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK got it, Here is where I am now. Why do I get an incompatible type error on lines 32 and 33. And how can I make it so it assigns the value it pulls from the string to my enum?

 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, substring returns a string, and neither guessedSuit nor guessedValue are of that type. So that's why you get an incompatible type message.
 
Tom Orr
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way to convert it from a string to my enum type? Like the wrapper classes?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I started to show you how I would do it in Java, and come to find out that Java has this built-in:



And that returns the right Enum of type Value.

In case you aren't aware of it (I wasn't), v.toString() will do the reverse -- return the String declared as the enum constant.

I recommend http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Enum.html -- lots of good stuff in there.

rc
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, toString() can be overridden to return something else. v.name() will always return the actual name, as it's a final method in java.lang.Enum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!