• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

learning java - today: enums

 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
SWEEEEEEET!

You have mae a grace mistake - I shall haunt this place with simple questions!
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic