wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Head First Into Java : 99 bottles of beer song Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Into Java : 99 bottles of beer song Question" Watch "Head First Into Java : 99 bottles of beer song Question" New topic
Author

Head First Into Java : 99 bottles of beer song Question

Jimmy Semaj
Greenhorn

Joined: Jun 24, 2007
Posts: 3
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

Joined: Jun 24, 2007
Posts: 3
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

Joined: Jun 13, 2010
Posts: 6
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
Welcome to the Ranch Dhawal Mehta
Dhawal Mehta
Greenhorn

Joined: Jun 13, 2010
Posts: 6
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
I presume you have run that code and verified that it is correct?
Dhawal Mehta
Greenhorn

Joined: Jun 13, 2010
Posts: 6
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
Don't understand; when control enters the while loop, the first lines inside it are executed first, not the innermost if.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Mar 12, 2013
Posts: 1


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

Joined: Oct 13, 2005
Posts: 39869
    
  28
Welcome to the Ranch
Any point in answering a three‑year‑old thread?
Leonard Mckenzie
Greenhorn

Joined: Jul 30, 2013
Posts: 1
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

Joined: Sep 30, 2013
Posts: 1
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

Joined: Mar 17, 2011
Posts: 8244
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Arun KumarS
Greenhorn

Joined: Oct 03, 2013
Posts: 5
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
Welcome to the Ranch
Ivan Addeo
Greenhorn

Joined: Feb 25, 2014
Posts: 18

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

Joined: Aug 05, 2005
Posts: 3739
    
  16
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.


Joanne
Ivan Addeo
Greenhorn

Joined: Feb 25, 2014
Posts: 18

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

Joined: Mar 17, 2011
Posts: 8244
    
  23

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
Greenhorn

Joined: Feb 25, 2014
Posts: 18

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

Joined: Oct 08, 2012
Posts: 135
Take a look at these to snippets.

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




Ivan Addeo
Greenhorn

Joined: Feb 25, 2014
Posts: 18

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

Joined: May 03, 2008
Posts: 4658
    
    5

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


luck, db
There are no new questions, but there may be new answers.
Ivan Addeo
Greenhorn

Joined: Feb 25, 2014
Posts: 18

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
Greenhorn

Joined: Feb 25, 2014
Posts: 18

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

Joined: Oct 11, 2014
Posts: 1
I'm new to Java. Here is my lite contribution
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8244
    
  23

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

Joined: Nov 14, 2014
Posts: 1
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
Welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Into Java : 99 bottles of beer song Question