Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Beginning Java and the fly likes what's wrong with the for-each Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what Watch "what New topic

what's wrong with the for-each

Bin Qian

Joined: Sep 13, 2006
Posts: 18
Hello everyone:
here is the code

the program calculates the average of sumOfNumber(a variable in the program) integers, and the sumOfNumber is enterd by users also the number;
when i use comment(1) it's ok,but it doesn't work when i use commnet(2), why???
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

By "doesn't work" do you mean doesn't compile? If so, check the version of Java you are using; the for : each syntax is only available from v5.0 up.

If by "doesn't work" you mean something else, can you lease explain what that is (by describing the behaviour and/or posting any stack trace you have)?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
The foreach-loop (as in //(2)) is equivalent to

There is no "reassignment" like "number[i]=nb".
Bin Qian

Joined: Sep 13, 2006
Posts: 18
ok !thank you everyone espatically Anton Uwe!
i see now!
Have you tried LearnNowOnline?
subject: what's wrong with the for-each