• Post Reply Bookmark Topic Watch Topic
  • New Topic

[HeadFirst Java] Sink a DotCom problem  RSS feed

 
Max Grossenbacher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just finished the sink a dot com game in Chapter 6 of HFJ. My problem is when I run the game it works but I went through every single possible cell and never had a hit. When I un-commented the line The console shows me this:

Your goal is to sink three dot coms.
Pets.com, eToys.com, Go2.com
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at GameHelper.placeDotCom(GameHelper.java:71)
at DotComBust.setUpGame(DotComBust.java:26)
at DotComBust.main(DotComBust.java:69)
Try to sink them all in the fewest number of guesses
try 40 try 10

Here is the code for the 3 classes I have created, and they do compile and even execute (when the Syste.out.print... is un-commented but not working properly.

DotComBust Class




DotCom Class





GameHelper Class

 
Max Grossenbacher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went through this forum and looked through all the code it looked the same as mine to me, but then i decided to copy and replace one class after the next one with my class. I have replaced all the classes and I finally when I replaced the GameHelper class to the one below I got this error:

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
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "alphaCells", delete this token
The method get(int) is undefined for the type String

at GameHelper.placeDotCom(GameHelper.java:74)
at DotComBust.setUpGame(DotComBust.java:26)
at DotComBust.main(DotComBust.java:75)


But again the error only shows up when I un-comment line 74:


 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never run anything with unresolved compilation problems. Sort out the compiler error first. I suggest you stop using Eclipse if you are a beginner.
If you are getting out of bounds exceptions for index = 0, it suggests you have managed to create a zero‑length array and you are trying to use element out of the none that it has. you should be able to work out which array it is from the line number given in the exception’s stack trace.
 
Max Grossenbacher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Never run anything with unresolved compilation problems. Sort out the compiler error first. I suggest you stop using Eclipse if you are a beginner.
If you are getting out of bounds exceptions for index = 0, it suggests you have managed to create a zero‑length array and you are trying to use element out of the none that it has. You should be able to work out which array it is from the line number given in the exception’s stack trace.


Thank you, for your reply so it's an issue with the array?
I usually use Notepad++ I only used eclipse for that to see if the debugger could help me. Thank you for pointing me into the right direction.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
You appear to have told something that alphaCells has a get method when it hasn’t got a get method. What type is alphaCells? You have it down as an ArrayList somwehere, and that definitely has a get method. Why does Eclipse think it is a String?
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your missing a + operator from this line of code:

How it should look:



How yours looks:



See the difference?                                                        
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!