aspose file tools*
The moose likes Beginning Java and the fly likes learning java - today: enums Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "learning java - today: enums" Watch "learning java - today: enums" New topic
Author

learning java - today: enums

Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
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


The problem people have in trying to create something foolproof is that they generally underestimate the inginuity of fools.
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
The is an example in the Sun tutorial here. That shows enum, have you tried that?
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
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

Joined: Oct 27, 2005
Posts: 19557
    
  16

Every enum class automatically gets a method called values() that returns an array with the values of that enum. Use that one.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
SWEEEEEEET!

You have mae a grace mistake - I shall haunt this place with simple questions!
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Great, but remember, during your Haunting, could you please use the code tags!


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: learning java - today: enums
 
Similar Threads
Scissors-rock-paper game looking for better way to write the program
How to insert enum type entity in MySQL with JPA
please explain every line
SPLIT METHOD IN STRING CLASS
what is Enum