Forums Register Login

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

+Pie Number of slices to send: Send
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!!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Any help folks?!
+Pie Number of slices to send: Send
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"
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks Dennis and Athira!

The problem was with not looking at the code from the right perspective. Thank you for the time!
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4937 times.
Similar Threads
Head First Jave - Code Example - NullPointerException
<identifier> expected
Head First Java DotCom, DotComBust, GameHelper
Head First Java P148 - DotComBust - compile errors
Head First Java Programs
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:42:19.