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
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!!
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.
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.
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.