• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Java Enums and Switch

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm struggling with using enums. You can see my code above. In displayMap, I'm trying to do a switch on the value of galaxyMap[x][y] which should be an enum value. However, I can't seem to wrap my mind around how to get this to work with a switch statement.
 
Saloon Keeper
Posts: 6757
63
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable 'item' that you are passing in to the switch() already tells the switch that it is dealing with a MapItem enum and so further identification of the enum constants with 'MapItem' is not necessary. Also, all of the MapItem constants must be accounted for in the switch statement.
 
A Diamond
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was it, thank you!
 
Marshal
Posts: 67437
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . Also, all of the MapItem constants must be accounted for in the switch statement. . . .

It is a good idea to use all the constants in the switch, even if the compiller doesn't enforce it. Some of the newer forms of switch may require you to use all the enum constants.
 
Carey Brown
Saloon Keeper
Posts: 6757
63
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Some of the newer forms of switch may require you to use all the enum constants.

Ah, maybe that's where I remember running into it. Thanks.
 
Campbell Ritchie
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . maybe that's where I remember running into it. Thanks.

That's a pleasure Even more of a pleasure if I turn out not to be mistaken
 
Carey Brown
Saloon Keeper
Posts: 6757
63
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Expression-with-arrow and expression-with-label require all enum values -OR- a default.
 
A Diamond
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, ha, ya, I was going to add them all. Wanted to get just one working first ;)
 
Campbell Ritchie
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, I suggest you start with all the constants; try this sort of variant of Carey's suggested block:-Once you have seen that all four options have been reached correctly, you can change the testing code (println) to what you actually want.

Since enum constants are objects, it is possible to build the required functionality into the enum by giving each element an appropriate method. That may be a more object‑oriented way to do things, and may make your switch unnecessary. There is an example about arithmetic somewhere in the JLS (=Java® Language Specification).
 
A Diamond
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've given me some food for thought! I'm going to play with the enums a bit more.
 
Campbell Ritchie
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds a good idea Have a look at the Java® Language Specification (=JLS), which has some tutorial‑like examples, as well as this Java™ Tutorials section.
 
It will give me the powers of the gods. Not bad for a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!