aspose file tools*
The moose likes Beginning Java and the fly likes quick question from the book Head First Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "quick question from the book Head First Java" Watch "quick question from the book Head First Java" New topic
Author

quick question from the book Head First Java

harry winkler
Greenhorn

Joined: Mar 29, 2012
Posts: 8
I have just started reading this book and got to the part where he/they ask you to create the 99 bottles of beer on the wall program . . . without looking at the authors code i wrote the following . . .



After i finished and it ran i went to look at the authors code and noticed that they had put the if statement inside of the white statement and had also declared a string variable for "bottles"

I realize how declaring the string variable for bottle will be more useful down the line when looking at the big picture but i was mostly wondering about their location of the if statement. If anyone happens to have this book laying around this is on page 14. Any advice or input would be appreciated. Thanks.


Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

I don't have that book, but I imagine that having "bottle" in a String variable would make it easier to change in the future to cans or kegs or whatever.

You have an if statement inside your while loop too. You use it to print out two verses of the song on the final loop iteration. I'd guess the book solution makes one more iteration than you do, and uses its if statement to change the output slightly for the final verse.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Welcome to the Ranch

Loops that count things like that can be very fussy to program. One tiny slip and you end up singing “Two bottle of beer on the wall . . .”
I trust you noticed the small print at the bottom of the page?
There’s still one little flaw. . . .
You have discovered that there are several ways to program things, and you have hit on a different way to get the same result. You do realise the if is redundant? Yes, you can lose the line with if and get the same output, as long as you make sure the {} match.
harry winkler
Greenhorn

Joined: Mar 29, 2012
Posts: 8
Campbell Ritchie wrote:Welcome to the Ranch

Loops that count things like that can be very fussy to program. One tiny slip and you end up singing “Two bottle of beer on the wall . . .”
I trust you noticed the small print at the bottom of the page?
There’s still one little flaw. . . .
You have discovered that there are several ways to program things, and you have hit on a different way to get the same result. You do realise the if is redundant? Yes, you can lose the line with if and get the same output, as long as you make sure the {} match.


i saw that statement and went through their code line by line but couldnt figure out what the flaw was. When i was debugging mine and changing it i ran into something that i thought might have been it but i couldnt remember exactly what it had been with my code. Without typing all of their code out and running it i dont think at this time i would be able to sit here and figure out what the flaw was just by looking at it. I didnt feel like running their code so i just moved on. If you think it is something very important and should go back and figure it out let me know and i will be sure to do that.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Greg Charles wrote:You have an if statement inside your while loop too.

His inconsistent indentation makes it look that way, but the if statement is actually outside the loop.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Campbell Ritchie wrote:One tiny slip and you end up singing “Two bottle of beer on the wall . . .”

Or perhaps "One bottles of beer on the wall".
harry winkler
Greenhorn

Joined: Mar 29, 2012
Posts: 8
Dennis Deems wrote:
Greg Charles wrote:You have an if statement inside your while loop too.

His inconsistent indentation makes it look that way, but the if statement is actually outside the loop.


could you please show me how my indention should be?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11246
    
  16

I think the only problem is with the if block. The way you have it indented, it looks like it is part of the while-loop body, but it isn't. It should be moved left...something like this:



Note that I use two spaces, while you use four. that isn't the issue, just a style choice.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
harry winkler wrote:
Dennis Deems wrote:
Greg Charles wrote:You have an if statement inside your while loop too.

His inconsistent indentation makes it look that way, but the if statement is actually outside the loop.


could you please show me how my indention should be?


Yours:


Preferred (statements at the same level have the same indentation):


I left the line breaks alone, but I'd encourage some adjustments there, as well -- though to a great extent this is a matter of taste. It seems to me the two blank lines that come after the opening of the while block make the actual code in the block look a little lonely.
 
Don't get me started about those stupid light bulbs.
 
subject: quick question from the book Head First Java