aspose file tools*
The moose likes Beginning Java and the fly likes Hang by Kathie and Bert on P. 14 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hang by Kathie and Bert on P. 14" Watch "Hang by Kathie and Bert on P. 14" New topic
Author

Hang by Kathie and Bert on P. 14

Mark Lopez
Greenhorn

Joined: Aug 30, 2005
Posts: 18
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

Joined: Feb 08, 2004
Posts: 1281
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


java amateur
Mark Lopez
Greenhorn

Joined: Aug 30, 2005
Posts: 18
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

Joined: Feb 08, 2004
Posts: 1281
your code prints ok; what seems to be the problem?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mark Lopez
Greenhorn

Joined: Aug 30, 2005
Posts: 18
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 30, 2005
Posts: 18
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 30, 2005
Posts: 18
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

Joined: Aug 30, 2005
Posts: 18
Look better. Thanks again

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
are you guys drunk?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Mark Lopez
Greenhorn

Joined: Aug 30, 2005
Posts: 18
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hang by Kathie and Bert on P. 14
 
Similar Threads
Beersong
Whos reading the "Head First Java" book currently?
quick question from the book Head First Java
Flaw in Program
Head first Beersong