• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

learning java - today: enums

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm trying tolearn java, didn't even know it was a prog language 5 days ago - so bare with me.

I have a boolean array, let's call it when:

boolean[] when = new boolean[7];

I have an enum that looks kinda like this:

enum weekday {
mon("Mondays suck!"),
tue("Tuesdays suck, too!"),
...
sun("Sundays don't suck so much.");
{

private final String s, this.s=s andsoforth.

Now: I want my program to output

for (i=0; i<7;i++) {
if (when[i]) sysout(...well, that's kinda the problem! How do I tell the program that when i=0 and when[0] is true to print weekday.mon? Should I write the enum differently? It would really be a little bit of a pain to change the boolean[], as it is used elsewhere in the program, but if that makes more sense: bring it on.

You said no question too simple...

Thanks a bunch, Martin
 
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The is an example in the Sun tutorial here. That shows enum, have you tried that?
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did, actually, I adjusted my own output to that example, because I didn't think too many people here spoke German.

I could, I suppose, do something like

switch(i) {
case 0 : sysout weekday.mon;
case 1 : sysout weekday.tue;
...
}

But that doesn't seem too elegant. I had the data in a string[] before, so I could just write

if when[i] then sysout weekday[i]

The friend who sat down with me for a couple of hours last Sunday to tell me what a class is said I should change it to enum - but now I'm not so sure. Seems more complicated to me...
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every enum class automatically gets a method called values() that returns an array with the values of that enum. Use that one.
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SWEEEEEEET!

You have mae a grace mistake - I shall haunt this place with simple questions!
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, but remember, during your Haunting, could you please use the code tags!
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic