This week's book giveaway is in the Agile and Other Processes forum. We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line! See this thread for details.
Im getting a bunch of errors trying to compile the DotCom game (deluxe version) in Head First Java. If you can help me with this, I wont be bothering this forum with hardly any more compile problems because the book doesnt have any more programs for the next 300 pages or so. The program is made up of 3 files: GameHelper.java, DotCom.java, and the third the book is not clear what the name is, I think its DotComBust.java. So far the only file that compiles without errors is DotCom.java. The errors that come up with GameHelper.java are "cannot find symbol" errors and DotComBust gets "class or interface expected" errors. Heres the code:
The first error message for DotComBust is going to complain about the startPlaying() method, expecting a class or interface. "Expected class or interface" always means that the compiler found something outside of any class that doesn't look like a class, which almost always means that you've got an extra close brace in there, which terminates the class early, which makes the later parts of the class look like "extra". And indeed, if you look at the setUpGame() method, we see
That line that says "close for loop" looks suspicious, as there is no for loop in that routine!
The indentation seems to suggest that your editor understood this problem, which is a good thing. Listen to the editor -- if the automatic formatting doesn't look properly nested, it generally means you've got an extra or missing brace or parenthesis, or a missing double-quote.
In GameHelper, some of the messages we be about not finding "gridLength". Look at the declaration of gridLength, remember that Java is case sensitive...
Reading Ernest's post will get rid of four of those errors (the getLength ones).
As for the second error. Did you really copy that line from a book ? If not, then think what you are actually trying to do. It will probably be better if you split it up into a couple of lines - the assignment and the test.
Thank you everyone, I got it to compile. Sorry for rushing out a response without carefully reading all the posts but I had to rush to class. Not being able to get programs to compile has been a major block in teaching myself to program, Im very appreciate of your help. I hope I will be able to catch all this stuff myself next time.
Originally posted by Brad Cantrell: ...Not being able to get programs to compile has been a major block in teaching myself to program...
Get used to it. Compiler errors are a fact of life, even for professional programmers. It is very annoying to spend half an hour trying to fix a compiler error to finally figure out that you just forgot a semi-colon somewhere.
Of course, I'm talking about compiling the code I wrote myself here. It's probably even more frustrating when code from a book won't compile. However, when this happens for me, my first assumption is that I typed it incorrectly. Of course, even then, it is difficult to find the error even when you are carefully comparing with the code listing from the book.
Anyways, I hope you are enjoying learning how to program in Java. Please feel free to ask questions here often. In particular, learning what the compiler errors mean is a great skill to develop.
Thanks for that comment Layne. Some people on this forum seem to have some kind of Jedi power for spotting bad code. With me not even being able to copy code out of a book I was starting to get discouraged as to my having ability to program.
Originally posted by Brad Cantrell: Thanks for that comment Layne. Some people on this forum seem to have some kind of Jedi power for spotting bad code. With me not even being able to copy code out of a book I was starting to get discouraged as to my having ability to program.
For most of us, this mysterious Jedi power comes from years of experience and seeing the same compiler errors hundreds of times. Like I said, learning what a compiler error means, and even more importantly, the most likely causes, makes life a lot less difficult and frustrating.
Originally posted by Brad Cantrell: ...Not being able to get programs to compile has been a major block in teaching myself to program...-Brad
I'm sorry to disagree. Not being able to get programs to compile is a very important phase in becoming a good programmer.
If you type over code from a book and it compiles and runs fine in one go, you will have learned a few things. If it doesn't compile however and you have to hunt for bugs and spend some, admittedly frustrating, time trying to get it to run and having to force yourself to really understand what is going on in the code and what all that compiler error gibberish means, then you will have learned a LOT.
u is a letter, not a word.
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards