• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Java Enums and Switch

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9735
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was it, thank you!
 
Marshal
Posts: 76862
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9735
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9735
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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's exactly the same and completely different as this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic