my dog learned polymorphism*
The moose likes Beginning Java and the fly likes DotComBustGame in HFJ -- not working; can't figure out what I've missed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DotComBustGame in HFJ -- not working; can Watch "DotComBustGame in HFJ -- not working; can New topic
Author

DotComBustGame in HFJ -- not working; can't figure out what I've missed

Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
I've typed the code for the DotComBust Game in HFJ, and it all compiled, but for some reason, I'm not getting the string prompt to enter a guess or the result of my guess (hit or kill). For those familiar with the game, here's the code. Can someone point out what's wrong? My head is swimming/ spinning from trying to figure it out.









The GameHelper code was ready bake, so if there's a problem somewhere in the getUserInput () method, I'm not seeing it, because I just typed it in an compiled it (it's ahead of what the chapter is teaching). It's supposed to prompt a guess with "enter a guess" and return a result of hit or kill, but after the instructions display, the cursor just blinks and when I enter a guess, nothing happens.

It's probably something small I missed, but I don't see it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Please don't use so many spaces before your //comments; that makes the code really difficult to read. You will have to use the "preview" button to see the formatting. Which page is this game on? HFJ seems to have several dot-com games in.

Don't know. You are going to have to put some print statements in, so as to follow the flow through your application. Example, new line 20½ in your setUpGame method, which ought to tell you there are 3 members:New line 27½ similar, but see how many members there are in the newLocation list. You will probably find it prints 1 regardless. Are you really supposed to create a new List every time you traverse that loop?
Similarly you can put print statements into the other methods and follow the control flow. A standard debugging technique. The debuggers on IDEs do the same sort of thing, but are quicker.
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
In GameHelper line 41, you're missing an ampersand. I haven't tested it, but maybe that's sending it into an infinite loop.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
I think the single & is intended. You have to increment the counter, so using a single & ensures both halves of the expression are evaluated.

Actually a && would make little difference; once you get false to the left of it the loop will terminate. You might get a guess count one less, but I shall leave you to work that out.
Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
I don't fully understand the code - some of the code the book hasn't gotten to yet; it simply said (tongue in cheek ) 'back slowly away from this code, except to type it in and compile it.' So at this point I wouldn't know where to begin to deciper/debug it. Like I said, it compiles fine, but after the instructions are displayed, the cursor just sits there, no prompt to enter the guess, and won't display a result when I enter a guess. I'm guessing the problem is somewhere in the getUserInput() method, but I'm not sure exactly where. I typed everything in correctly, so perhaps the problem is the code itself, and it's something I not able to detect because I don't know enough?

P.S. Sorry about the spaces, I was following the format in the book.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

You should go to the website and download the GameHelper class. If you just have a typo in the GameHelper then it will work. Be sure to comment out the package information on the top.

If it doesn't work after that, you know it's your code... i.e. the parts you should know how they work. I'll see what I can do with it, but I would start there.


When you do things right, people won't be sure you've done anything at all.
Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
Can you tell me which website -- do you mean wickedly smart or the HFJ website (the Head First Series website). There were some downloads there but the were only the DotCom and DotComBust class. I couldn't find the GameHelper class for it on that site.

If you mean wickedly smart website, then there was a link to a download for some JAR files. I downloaded it, but when I tried to open it, it said "The Java JAR file "codeKitechesTiger-1.jar" could not be launched. Check the Console for possible error messages." I'm unfamiliar with JAR files (haven't gotten there yet).

P.S. I found the code online (not the headfirstlabs site) and just copied it into a file. It works now, so I guess I had some kind of typo with my code that I couldn't see. Doh!

I went to the headfirstlabs site and found that link, but it only downloaded the DotCom and DotComBust code.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

http://headfirstlabs.com/books/hfjava/

Scroll down and there's code by chapter....
 
jQuery in Action, 2nd edition
 
subject: DotComBustGame in HFJ -- not working; can't figure out what I've missed
 
Similar Threads
Head First Java Programs
Head First Java P148 - DotComBust - compile errors
Head First Java DotCom, DotComBust, GameHelper
game compile problems
Head First Jave - Code Example - NullPointerException