File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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: 37958
    
  22
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: 37958
    
  22
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: 37958
    
  22
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: 37958
    
  22
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: 37958
    
  22
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: 7529
    
  18

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: 37958
    
  22
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
 
Similar Threads
Beersong
head first java beer song program help
Head first Beersong
Flaw in Program
Exception in thread"main"java.lang.NoSuchMethodError.main