Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Into Java : 99 bottles of beer song Question

 
Jimmy Semaj
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I am quite new to the java programming language (as well as programming in general) so i picked up the Java Head First book for JAVA 5. Going through the examples i am having problems correcting the "99 bottles of beer" problem on page 14 of the second edition of the book. The code compiles and works but there is a snippit below that says " there's still one little flow in our code. It runs, but it's output isn't 100% perfect. See if you can spot the flaw, and fix it.". I have been working on it for the better part of a half an hour and i cannot fix it. I believe the problem is at the 1 bottle of beer part of the output. Daniel Morrison mentioned in and earlier post:
Yes, and thanks. I also found the other flaw put there by the author:

code:

if (beerNum == 1) {
word = "bottle"; //single bottle of beer
}


should come after...

code:

beerNum = beerNum - 1;



I must be doing something wrong since it does not seem to work for me. The following code is before i started screwing it up. My last rendition actually froze up the compiler for a long while. I have been compiling in netbeans ide5.5 since the command line does not seem to want to always compile and the IDE can somewhat help me find my mistakes. Mistakes i have found have been normally spelling.


[ June 24, 2007: Message edited by: semaj semaj ]
 
Jimmy Semaj
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what I may have been missing but i started fresh and the code worked this time. Maybe this topic can be used for reference for people.

 
Dhawal Mehta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Everybody.

Jimmy is right. we have to place the first If loop after statements ans the output got correct.

One thing more I noticed that when control enter while loop it first go to the If loop and print the statement under this loop first:

if (beerNum > 0) {
System.out.println(beerNum + " " + word + " of beer on the wall");
}

and the goes to the while loop statements. Am i correct?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Dhawal Mehta
 
Dhawal Mehta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your warm welcome Campbell..

May you please check my previous post and tell me if i am correct....
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have run that code and verified that it is correct?
 
Dhawal Mehta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes .. I have run this code already...

but what i found is that when the control goes to 'while' loop.. it first executes the most inner 'if' loop and then execute the other instructions..

Is that correct?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't understand; when control enters the while loop, the first lines inside it are executed first, not the innermost if.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: there doesn't appear to be an inner if nor an outer if in that example; the two ifs are adjacent, not nested.
 
shafkat majeed
Greenhorn
Posts: 1
Google Web Toolkit Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public class BeerSong{
public static void main(String args[]){
int beerno=99;
String name="bottles";
while(beerno>1){
System.out.println();
System.out.print(beerno + " " + name + " " + "of bear on the wall,");
System.out.println(beerno + "" + name + " " +"of bear" );
System.out.print("take one down,pass it around,");
beerno--;
if(beerno>1)
System.out.println(beerno + " " + name + " " + "of bear on the wall");
else
{
name = "bottle";
System.out.println( beerno + " " + name + " " + "of bear on the wall");
}//end if
}//end loop
System.out.print(beerno + " " + name + " " + "of bear on the wall,");
System.out.println(beerno + " " + name + " " + "of bear" );
System.out.println("take one down,pass it around,");
System.out.print("no more bottles of bear on the wall");
}//end main
}//end class





 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Any point in answering a three‑year‑old thread?
 
Leonard Mckenzie
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch
Any point in answering a three‑year‑old thread?



Hello, I'm new to this forum and haven't programmed in 10 years since my undergrad days using C++ .Here is my take:

I spotted the issue but as soon as I fixed it another problem arose but I was able to solve the program w/o any errors.

Let me know if anyone is still experiencing issues.

 
Robert Archer
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main(String args[])

{
//Display title of the lyric
System.out.println(" 99 bottles of bear lyric ");
System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
//Display the lyric
for (int i=99; i> 0; i--)
{
if (i >= 3) //handle 3 or more bottles - display 3 or more bottles of bear...
{
System.out.print(i + " bottles of beer on the wall; " + i + " bottles of ober; take one down, pass it around; ");
System.out.println( (i - 1) + " bottles of bear on the wall.");
}
else if (i == 2) //handles 2 bottles of bear - display 2 bottles of bear ...
{
System.out.print(i + " bottles of beer on the wall; " + i + " bottles of beer; take one down, pass it around; ");
System.out.println( (i - 1) + " bottle of bear on the wall.");
}
else //handle 1 bottle of bear - display 1 bottle of bear ...
{
System.out.print("1 bottle of bear roar on the wall, ");
System.out.println("1 bottle of bear careful!; take one down, pass it around, no bottles of bear on the wall everyone's malled.");
System.out.println("No bottles of bear on the wall. and that is why you need to read the code. So bears don't eat you!");
}
}

}
}
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert Archer wrote:public static void main(String args[])

First: Welcome to JavaRanch, Robert.

Second: Not sure if that was meant to be a solution or not; but if you post code, please read the UseCodeTags (←click) page - in particular, the bit about NOT WRITING LONG LINES.

Third: If it was meant to be a solution, also check out DontBeACodeMill (←click). In fact, you might want to give the whole HowToAnswerQuestionsOnJavaRanch page a read.

Winston
 
Arun KumarS
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The program is working correctly.

Lyrics of the song 99 Bottles of Beer:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.
.
.
.


The possible correction might be

System.out.println("Pass it around."); >> here the println should be replaced by print
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Ivan Addeo
Ranch Hand
Posts: 35
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading this book now, i discovered that the lyrics of the song're "99 bottles of beer on the wall / 99 bottles of beer. / Take one down / Pass it around".
But in the book's program the string "bottles of beer on the wall" is repeated two times. So i modified the program in this way:


Bye
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:I'm reading this book now, i discovered that the lyrics of the song're "99 bottles of beer on the wall / 99 bottles of beer. / Take one down / Pass it around".
But in the book's program the string "bottles of beer on the wall" is repeated two times. So i modified the program in this way:

It should be repeated twice. The first line of each verse is the same as the last line of the previous verse.

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

etc.
 
Ivan Addeo
Ranch Hand
Posts: 35
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Ivan Addeo wrote:I'm reading this book now, i discovered that the lyrics of the song're "99 bottles of beer on the wall / 99 bottles of beer. / Take one down / Pass it around".
But in the book's program the string "bottles of beer on the wall" is repeated two times. So i modified the program in this way:

It should be repeated twice. The first line of each verse is the same as the last line of the previous verse.

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

etc.

But the program in the book has not this output.. how should be the code?
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:But the program in the book has not this output.. how should be the code?

Ivan,

Don't worry about it. Write the program as you've been asked to.

And "how should be the code?" is one question we will not answer. We will help you to write it; but we won't do it for you.

So: forgetting that extra line, does your program work?

Winston
 
Ivan Addeo
Ranch Hand
Posts: 35
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Ivan Addeo wrote:But the program in the book has not this output.. how should be the code?

Ivan,

Don't worry about it. Write the program as you've been asked to.

And "how should be the code?" is one question we will not answer. We will help you to write it; but we won't do it for you.

So: forgetting that extra line, does your program work?

Winston

Yes it works, now i'm working on the code.

EDIT: Ok, it works properly now (except for the "0 bottles" output and "1 bottles" in the "2 bottles" block , i don't want them):



I solved my second problem in this way:

 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at these to snippets.

The while loop only runs when the variable is greater than zero.




 
Ivan Addeo
Ranch Hand
Posts: 35
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Gillespie wrote:Take a look at these to snippets.

The while loop only runs when the variable is greater than zero.





Yes, but the program do the subtraction '1-1' and than output "0 bottle of beer on the wall" and after "No more[...]".

I'm working on a solution
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:Yes, but the program do the subtraction '1-1' and than output "0 bottle of beer on the wall" and after "No more[...]".

I'm working on a solution


Hint: MessageFormat
 
Ivan Addeo
Ranch Hand
Posts: 35
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
Ivan Addeo wrote:Yes, but the program do the subtraction '1-1' and than output "0 bottle of beer on the wall" and after "No more[...]".

I'm working on a solution


Hint: MessageFormat

I read, but i'm still confused I don't want the "0 bottle" output, but only the "No more[...]".
 
Ivan Addeo
Ranch Hand
Posts: 35
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved, i remembered the use of 'break' and i used it



But why the code at line 15 doesn't display an empty line?
 
mirko ninja
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to Java. Here is my lite contribution
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:I read, but i'm still confused I don't want the "0 bottle" output, but only the "No more[...]".

To me, this is fairly similar to the business of "ordinality" - ie, translating 1, 2, 3 to "1st", "2nd", "3rd".

What about a simple method that returns the right String? viz:That separates the problem from the rest of your code - a very good habit to get into.

Winston
 
sandy nambi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mirko ninja wrote:I'm new to Java. Here is my lite contribution
hey buddy you are using the first if in the wrong way..
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Bill foster
Ranch Hand
Posts: 57
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my version of the beer song:



Just wanted to share, my output from jGRASP is:

3 bottles of beer on the wall
3 bottles of beer.
Take one down.
Pass it around.
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 bottle of beer on the wall
*************************************
1 bottle of beer on the wall
1 bottle of beer.
Take one down.
Pass it around.
0 bottles of beer on the wall
*************************************
0 bottles of beer on the wall
0 bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.

----jGRASP: operation complete.
 
viktor lipovskyi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sandy nambi wrote:
mirko ninja wrote:I'm new to Java. Here is my lite contribution
hey buddy you are using the first if in the wrong way..



perhaps it will be so correct:
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post. Always use the tags: doesn't it look bettre
Why have you got
word = "bottle";
twice?
 
viktor lipovskyi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, when beerNum = 2, we need to have the next output of the last line:

"1 bottle of beer on the wall"

instead of

"1 bottles of beer on the wall"

I think so)))
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you have written it twice. Line 9 and line 21.
 
viktor lipovskyi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if I haven't done that it will not work((

Ok, what about this:



I think it works))
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have tried that code and know it will work.
You do not need lines 15 and 19. You only need that code once. You need
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move line 21 after the end of the while (beerNum > 0) loop.
 
viktor lipovskyi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Move line 21 after the end of the while (beerNum > 0) loop.


Thanks a lot for your patience))
I guessed it later, but you were the first!!!))
I am impressed and delighted JAVA
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic