• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hang by Kathie and Bert on P. 14

 
Mark Lopez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I�m new to programming. I see people over the Net talking about Java and make me want to learn. Then I bought a book, Head First Java, and am �block� in page 14.

It�s about trying to fix the following program BeerSong. It supposed to print first 99 bottles of bear on the wall until reach 1 bottle and No more bottles. But the program begging with 59 and other flaws.

Can you help with little tips (another that still reading in other pages of the book)?

Also, as you see, I need to improve English Language. I try to learn both here.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you help with little tips (another that still reading in other pages of the book)?


in first place welcome to the world of java!

i guess the best for you is to post here the code you have so far, so that ranchers can help you out

please dont forget to use UBB Code to format your code
 
Mark Lopez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Miguel:

How much things I need to learn in so little space of time. I am going to revise what is UBB Code immediately.

Let me see what happen doing that.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code prints ok; what seems to be the problem?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As miguel noted, your code works fine.

However, there is a limitation on the number of lines that the Windows Command Prompt will display, and this is why your output appears to be starting at 59. If you combine some of your println statements, you will be able to see the entire output.
 
Mark Lopez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help.

But why the program jump on the second if statement if beerNum is greater than 0 and isn�t 1 yet?. Maybe the first if need something an else, like a second if? But then, in the next cycle jump disorderly in the firs if.

Example of the first three of the output:

Take one down.
Pass it around.
59 bottles of beer on the wall
59 bottles of beer on the wall
59 bottles of beer.
Take one down.
Pass it around.
58 bottles of beer on the wall
58 bottles of beer on the wall
58 bottles of beer.
Take one down.
Pass it around.
57 bottles of beer on the wall
57 bottles of beer on the wall
57 bottles of beer.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Lopez:
... why the program jump on the second if statement if beerNum is greater than 0 and isn�t 1 yet? ...

Just move your statement "if(beerNum == 1)..." to immediately follow the line "beerNum = beerNum-1;" This way, it will change the word to singular immediately after the count drops to 1.
 
Mark Lopez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear marc, you are joking like Kathie and Bert?

Please, give me the hint. If not I will need to continue reading the book to find the answer later.

With the code I show you above it happened this way. Look at the output of the final, bottle.

2 bottles of beer on the wall
2 bottles of beer on the wall
2 bottles of beer.
Take one down.
Pass it around.
1 bottles of beer on the wall
1 bottle of beer on the wall
1 bottle of beer.
Take one down.
Pass it around.
No more bottles of beer on the wall
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code looks like this now...

So when beerNum reaches 1, the next thing that happens is a println using the "word" variable, which is still "bottles." Then it goes back to the top of the while loop, word is changed to "bottle," and another println executes.

You can fix this by moving the code that changes the word variable...
 
Mark Lopez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark, as you said in the first. Merging the println resolve most of the flaw.


First and last of the output:

C:\JavaPrograms>java BeerSong1
99 bottles of beer on the wall.99 bottles of beer.Take one down.Pass it around.
98 bottles of beer on the wall
98 bottles of beer on the wall.98 bottles of beer.Take one down.Pass it around.
97 bottles of beer on the wall
97 bottles of beer on the wall.97 bottles of beer.Take one down.Pass it around.
96 bottles of beer on the wall

...

2 bottles of beer on the wall
2 bottles of beer on the wall.2 bottles of beer.Take one down.Pass it around.
1 bottles of beer on the wall
1 bottle of beer on the wall.1 bottle of beer.Take one down.Pass it around.
No more bottles of beer on the wall

C:\JavaPrograms>


Thank you!
 
Mark Lopez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look better. Thanks again

 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you guys drunk?
 
Mark Lopez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bert, no kid, your book is original, a masterpiece. And I�m not drunk yet. What next in "revolutionary" (with conservative images) approach to teaching and make a product for success?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic