This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enum  RSS feed

 
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
 
Sheriff
Posts: 23507
47
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.
 
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}

 
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?

 
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
 
Sheriff
Posts: 21330
87
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.
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!