wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Head First Java SimpleDotCom program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java SimpleDotCom program" Watch "Head First Java SimpleDotCom program" New topic
Author

Head First Java SimpleDotCom program

James Conroy
Greenhorn

Joined: Feb 11, 2012
Posts: 1
Hi,
I am having a problem with the SimpleDotCom program in the Head First Java book (chapter 5). I have completed chapter 5 and my program (basic version) works correctly except for one bug. Here's my source code:







When I run the program, it tells me how many guesses I've taken after every data input instead of at the end when isAlive is false and the ship is killed. Let me show you an example:



What am I doing wrong?

Many thanks in advance,
James.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Hi James, Welcome to JavaRanch,
There's subtle error in the code. If you dont specify the {} with the if-else statement then the compiler considers the immediate next statement as part of the if statement and statements following the immediate statement are not considered as part of the if-statement. If you have multiple statements in the if-else block, put them in the {}. But generally its more readable to separate such if-else constructs using the {}


Mohamed Sanaulla | My Blog
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

I would suggest you to read this section from the Java tutorials which cautions the programmers to such kind of errors.
Ali MacK
Greenhorn

Joined: Oct 09, 2013
Posts: 2
Hi - apologies for bumping an old thread but I'm experiencing exactly the same bug as OP. Identical code, identical bug.

Mohamed mentioned an error with {} braces but didn't really elaborate much further than that. Does the if statement require additional brace/s or fewer? Shouldn't the break statement eliminate moving on from the loop?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Welcome to the Ranch
Many style guides, e.g. this one and this one recommend always using braces after if, or other keywords followed by blocks. The reason is the languages with C‑derived syntax don't use END keywords. In many languages, e.g. Eiffel, you would write an if-else like this:-In that case you have to write THEN and END after IF, and those keywords delimit the bounds of the “if statement”. It is also obvious to anybody reading how many instructions are included in the THEN and how many in the ELSE. C‑derived syntax does something different; it uses brace pairs as delimiters, which can cause confusion. If you put several lines in {} the compiler will treat that as one statement, then later break it into separate parts. So if you want several lines to be executed after if, you have to put them in {}. If you don't use braces, you will have the first line only executed. Which can be even more confusing if you make the common mistake of adding a semicolon in the wrong placeIn that case the compiler will interpret that as if…do nothing. Whatever is in line 2 is not part of the if, but an ordinary line which is executed normally after the nothing has been done.

In the case of the break; written without {} after if: The one line after if is the body of the “if statement”. When you reach the break; you have completed the “if statement”, and the break; is executed regardless.

Does that help?
Ali MacK
Greenhorn

Joined: Oct 09, 2013
Posts: 2
Hi CR, thanks for the welcome and the response.

Welp, to be honest I was still none the wiser for reading your response and linked articles so I deleted the whole lot and typed it out again, and I'll be blowed, it works now.

Thanks again, I have little doubt that I'll be back!

This is a great resource that you guys are providing, much appreciated.

Cheers,

Ali
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
You're welcome Sorry it didn't help. Please ask if you would like somebody else to explain things; I find that sometimes, person “a” can explain things really badly and then person “b” explains it really well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java SimpleDotCom program
 
Similar Threads
Hi All, While working with Head first java, I got into a problem, please help me to solve this.....
return values in methods
This code of mine is not working, Please help.... i am studying from Head first java
getting exception in thread "main" java.lang.NoSuchMethodError: main error message
HFJ 2nd - Problem with SimpleDotCom