• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HeadFirst DotComBust... Help?

 
Brian Hazzard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently learning java by way of the book Head First Java. I'm at the point in the book where we are doing the DotComBust example game (its just like battleship). If you are familiar with this book example, or just know what the problem below is, please help me out. I have gotten all the bugs out except for the ones listed below:

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you're missing some brackets. Can you post the whole class ?
[ April 26, 2007: Message edited by: Satou kurinosuke ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

These all look like the result of a simple typo, like a missing brace or something. Can you post your code?
 
Brian Hazzard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies. Of course I will post my code, which is also available in Head First Java:



If you see something I've neglected, or need to see my other classes, just let me know.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this.




Check to make sure that the brackets are matched.
 
Brian Hazzard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thanks again for your help. It turns out that you were right. There was a missing bracket after the else in the if/else statement. I found it only moments after I posted the code... silly me.

Thanks so much for your help guys (girls?)!
 
Brian Hazzard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks keith, for keeping it vague so its a challenge. I appreciate that because I am here to learn!!
 
Brian Hazzard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While we are on the topic though... I had a similar problem also when I was developing it in my simplified version. I got this error code:



and here is the SimpleDotComGame class:

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the scope of result?

Also, as a heads-up, how will you break out of your while loop?
[ April 27, 2007: Message edited by: Fred Rosenberger ]
 
Brian Hazzard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the dotCom class the .checkyourself method sets the isAlive boolean to false if it is a kill, thats when the while breaks.

As far as scope of result... I honestly don't understand what you mean. I am new to the language, could you please explain what scope is?

Thanks,
Brian
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which text editor are you using? Get one which supports context highlighting and bracket matching. It makes that sort of error much easier to find.
If you are on Windows, try JCreatorLE which is a cut-down IDE, but you can use it as if it were a text editor.
On Linux there are excellent products like Kate or Gedit built in.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scope basically refers to where a variable exists. So when i say

int fred;

that variable fred doesn't exist until the program dies, it exists until it "goes out of scope".

Generally speaking, a variable goes out of scope when you hit the closing curly bracket that matches the opening bracket before the declaration.

so, you have this block of code:



Since the String result is declared inside this block (the bracket on the while line through the bracket just after NumOfGuesses++), it goes away after that last line. So when you get to your if (result...) line, result does not exist anymore. That's why the compiler is saying "I don't know what result is!!!".

try changing it to this:


Note: you will probably have to make other changes as well, but i didn't want to give it all away.
[ April 27, 2007: Message edited by: Fred Rosenberger ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic