Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Head First Java DotCom, DotComBust, GameHelper

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only have one error and I can't find a solution for it here is the code.



one file



second file



third file

the error i get is this

DotComBust.java:45: cannot find symbol
symbol : variable dotComToTest
location: class DotComBust
dotComsList.remove(dotComToTest);
^
Please help.


[Edit: put code between [code] and [/code] ubb code blocks]
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the error message says, the problem is in line 45 of DotComBust.java. The variable dotComToTest cannot be found because you don't define it anywhere.

For some reason you have a for-loop using an int x just before it, rather than for (DotCom dotComToTest : dotComsList) as in the book. Are you using a version of Java previous to 1.5? If so, you'll have to reference the list element using the index as you did a few lines above. If not, use the proper for-loop syntax as in the book.
 
jake dickens
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i read the book again and it said for(DotCom dotComToTest : dotComsList) and all to it but it still says dotComsList.remove(dotComToTest) is still wrong


also i'm using java the newest one 1.6.26.
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe try saving your file before you re-compile?
 
jake dickens
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the book is wrong on it according to the online version of the code and the downloaded one has the GameHelper imported but does not show it in the book.
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The online code doesn't work so you conclude the book is wrong?
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message you are getting is very explicit:

jake dickens wrote:DotComBust.java:45: cannot find symbol
symbol : variable dotComToTest
location: class DotComBust
dotComsList.remove(dotComToTest);



I've pulled out the method that this is referring to. The line numbers are now different, but line 45 in the original source code is equivalent to line 11 in the method shown below:



The error message is telling you that the first time it found the symbol dotComToTest was in line 45 in your code (line 11 above). Does that make sense to you now? Can you see anywhere else in that method where dotComToTest is mentioned (or, more importantly) defined?
 
jake dickens
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i downloaded the chapter 6 and compared it to mine and what you said was to make sure but i'm saying the book is wrong cause the book told me dotComToSet not ToTest
 
Andrew Monkhouse
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct, by the way - there is a mistake in the book.

You can find the Errata for Head First Java online, and search for dotComToTest - the problem with the printed copy of the book is mentioned about a third of the way down the page.

As an aside, I recommend you look for errata anytime you look at any technical book.
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm, that's bad if both the book and the online code are wrong!

My copy of the book is correct, at least. Sounds like you're not so lucky.
 
jake dickens
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Online is fine the book i have is incorrect

the online is correct

this is correct

I looked at the online code i had two lines wrong i fixed them.
 
Andrew Monkhouse
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jake

Please put code between [code] and [/code] ubb code blocks, otherwise it is too hard to read, and you will find many people will simply move on to easier questions. I fixed your first post for you - you can hit the edit button on that post to see what I have done (or read the link in this post).

As I noted above, this error has been identified in the errata - you are advised to look at it.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, jNewb here as well. I have built the java files like the book 2e + Errata has defined. My code compiles fine, but the output shows that the locationCells are not being set properly. I figure it is something to do with the placeDotCom setter or setLocationCells, but I also noticed that I don't see anything about setting the cells to an array with randomNum locs like in previous versions of the DotComGame. I will include the 3 java files as well as the printed output. All and any help is appreciated.

GameHelper.java



DotCom.java



DotComBust.java



Printed Output:

$ java DotComBust
Your goal is to sink three dot coms.
Pets.com, eToys.com, Go2.com
Try to sink them all in the fewest number of guesses
try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0 try 0

coord 1 = a0 coord 2 = a0 coord 3 = a0

try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0

coord 1 = a0 coord 2 = a0 coord 3 = a0

try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0 try 0 used 0

coord 1 = a0 coord 2 = a0 coord 3 = a0

Enter a guess a0
hit
Enter a guess a0
hit
Enter a guess a0
Ouch! You sunk Pets.com : (
hit
Enter a guess a0
hit
Enter a guess a0
hit
Enter a guess a0
Ouch! You sunk eToys.com : (
kill
Enter a guess a0
hit
Enter a guess a0
Ouch! You sunk Go2.com : (
kill
All Dot Coms are dead! Your stock is now worthless.
It only took you 8 guesses.
You got out before your options sank.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sean Hoar,

IN your game helper class you left out brackets in the while loop

while ( !success & attempts++ < 200 ) {
location = (int) (Math.random() * gridSize);
System.out.print(" try " + location);
int x = 0;
success = true;

this threw the calculations off, as computers read it from right to left
 
Marshal
Posts: 74684
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch , Sean Hoar

Please don’t use such long lines inside the code tags; I have had to put new lines in to make them legible.

Steve Bradshaw, what do you mean about right to left? That sounds incorrect. What you actually mean is that the cast has a higher precedence that multiplication, so (int)a * b has to be changed to (int)(a * b). That is a common mistake; if you cast the random number to an int like the first attempt, you always get 0.
 
Campbell Ritchie
Marshal
Posts: 74684
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But well done finding that line
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, I know that it has been like 6 years since all these discussions were around, but I am now reading the book and struggling with another problem found in the code.
I don't get any errors, but I can't find the error in the code's logic.
I would never get a 'Hit!!!' or 'Kill!!!' no matter what number I input. Seems like this method is merely not works out for some reason:



I am trying to dig into code, but I see no mistakes. Maybe you can help me if you guys are still around...
 
Alex Lychak
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, actually, it seems I didn't get the rules clear. I was trying to hit them using only integers, instead of using the correct -'a1'- like format.
The code turns out to be working fine. Just needs some usability boost...
 
Campbell Ritchie
Marshal
Posts: 74684
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done solving your problem and telling us the answer

. . . and welcome to the Ranch
 
Alex Lychak
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell-Ritchie, thanks a lot. I sincerely hope Ranch will help me move on with my Java knowledge and experience gaining
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic