• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

object array compile error

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm having compilation problems with this piece of code. The problem is with the 'x' in the lines arrayOfCars[x] = tempBuyCar; and System.out.println(arrayOfCars[x]);

i get the error 'cannot resolve symbol'. however if i replace the x with a number it compiles.

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one is easy. since x is declared inside the "for" statment, it falls out of scope once you leave the loop. you don't have braces after your "for" statement so, the only line that is part of your loop is the "tempBuyCar" line. that is the ONLY line being run over and over.

so...

when you get to the arrayOfCars, x has fallen out of scope, thus the compiler can't resolve it.

do this:

and life should be good.
[ May 20, 2004: Message edited by: fred rosenberger ]
 
Bob Joness
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep that fixed it. can't believe how simple it was.

cheers
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some beginners, and some folks with a bit of experience, like myself, always include opening and closing braces, with constructs such as for-loops and if-statements.

It's also a required/recommended practice by some coding syle guides, such as JavaRanch's style guide. The reasoning for this is that consistency is easier to read, less editing is involved if lines of code are added or removed, plus it becomes very obvious what code is associated with the construct.

Also, I strongly recommend that you make a practice of properly indenting your code, as this will strongly help in its readability.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic