This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

java 1.5 for loop problem  RSS feed

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm used to previous versions of Java so I was surprised to see the following in my new Java book:

int[] locationCells;
for (int cell : locationCells) {
//some code
}
When I tried it out my compiler complained with the following:

I've double checked and the only difference between this compiling and not is commenting out this statement.

java version "1.5.0_04"

Is the code in the book wrong or is something wrong with my installation of java or what?


[ September 17, 2005: Message edited by: Ernest Friedman-Hill ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code will not compile because 'locationCells' is not 'definitely assigned' (JLS 16 if I remember rightly). This is a different reason to the one that you have described. Provide a full source code sample along with your actual output (assuming a compile-time error) and expected output (assuming no compile-time error) if you want elaboration on your problem, since as it currently stands, your description is inaccurate.

http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an interesting error. Perhaps it is because you have not initialized the array, which is what I think Tony means by "not definitely assigned".

Layne
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use a local before it has been 'definitely assigned' as per Java Language Specification Chapter 6 Definite Assignment.
 
Karina Guenther
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replies. I will look things over and try again. I will be out of town for a week so it will take me awhile before I know whether that's the source of the problem, but it makes sense to me.

Kari
 
Karina Guenther
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I thought that I'd go ahead and show you the entire source code incase anyone wants to work with it while I'm gone. Remember the only thing different between it compiling and not is commenting out that statement.



(Marilyn added code tags)
[ September 18, 2005: Message edited by: Marilyn de Queiroz ]
 
Sheriff
Posts: 9087
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Are you using a java 5 compiler? I'm not having any problem compiling the code other than an extra curly brace that I found.
[ September 18, 2005: Message edited by: Marilyn de Queiroz ]
 
Karina Guenther
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally got it to work. The problem was that I had an old version of Java 1.4 kicking around as well as Java 5. Although my java -version returned a value of Java 5, the compiler was looking at Java 1.4. I still have my registry saying I have java 1.4, but the files are gone now.

Thanks to everyone for your help.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac -J-version for next time.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!