File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Head First Java sink a dot com game, where am I going wrong? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java sink a dot com game, where am I going wrong?" Watch "Head First Java sink a dot com game, where am I going wrong?" New topic
Author

Head First Java sink a dot com game, where am I going wrong?

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Folks,

Im trying to learn Java through HeadFirst and tried out some of their code on my own to build a sink-a -dot-com game. The game is simple, its supposed to take three user inputs and compare them as to whether they match those of the cells alloted to the dotcoms randomly.
Here's my source code

DotComBust.java


and DotCom.java



When I run DotComBust.java this is what happens

Im pretty sure the problem is because of the element not being present in the list, then how am I being able to print hit? Any insight would be helpful, thanks!!

Learning and Learning!-- Java all the way!
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
The if/else block that prints "hit" or "kill" should be nested inside the if block that updates the list. That way it will only execute if a hit has occurred. As it stands, this code is executed whether or not it's a hit or a miss.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for the reply Dennis.

I moved the if/else block into the if statement, however the program still doesnt work as expected.For instance, even if an element is actually present in the 'dotcom' , the program still results in a miss ,I some how feel that Im not being able to set the locationCells correctly in DotCom.java. Any help on that would be great.

method that sets up the Game in DotComBust


Result
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Any help folks?!
Athira Vineeth
Greenhorn

Joined: Sep 06, 2010
Posts: 16
hi

i think it is because


locationCells is not empty so it is always printing hit. in the first code

after changing the if(index>=0) position ,the execution never entered into if(index>=0){ block

so the default value got printed "miss"
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
It's time to learn the art of debugging. You need to test your assumptions by printing things to the console (or using the debugging tool if you're using an IDE). You already know that locationCells does NOT contain userGuess. That's your starting point to focus your debugging efforts. How do things get added to the list? What are the contents of the list when you call indexOf? This is the real blood sweat and tears of programming, and only you can do it.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks Dennis and Athira!

The problem was with not looking at the code from the right perspective. Thank you for the time!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java sink a dot com game, where am I going wrong?
 
Similar Threads
<identifier> expected
Head First Jave - Code Example - NullPointerException
Head First Java P148 - DotComBust - compile errors
Head First Java Programs
Head First Java DotCom, DotComBust, GameHelper