Iterate through enums in Java using for-each?

public enum Wochentage { MONTAG(100), DIENSTAG(200), MITTWOCH(300); private final int value; Wochentage(int i) { this.value = i; } public int getValue() { return value; } }

I now want to output the values โ€‹โ€‹of the enum in the main using for-each:

 for (Wochentage w = Wochentage.values()) { System.out.print(w + " "); }

I'm getting an error with weekdays.values(). I honestly don't understand why a semicolon is missing.

1 vote, average: 1.00 out of 1 (1 rating, 1 votes, rated)
You need to be a registered member to rate this.
Loading...
Subscribe
Notify of
11 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
fraktalismanni
1 year ago

What you mean looks more like for each, doesn't know how it is in Java meanwhile, but formally I think your syntax is an incomplete for loop.

MrAmazing2
1 year ago
for (Wochentage w : Wochentage.values()) { System.out.print(w + " "); }
MrAmazing2
1 year ago
Reply to  RedDevil1982

No, there's a = instead of the double point…

for-each loops need a double point.

jakob960198
1 year ago
Reply to  RedDevil1982

Check it out

MrAmazing2
1 year ago

Nice, nice my name leaked haha

jakob960198
1 year ago

Hi Flo

MrAmazing2
1 year ago

yes, fits ๐Ÿ‘๐Ÿผ

MrAmazing2
1 year ago

Sure. ๐Ÿ˜€