wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Beersong Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beersong" Watch "Beersong" New topic
Author

Beersong

alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Hello Everyone, I am new to Java I am reading a book called Head first Java. I am in the first chapter, The book is is talking about a Beersong which I am Having difficulties understanding. It is telling me that the program m has a Flaw. It is saying that the program will compile and run but the output is not 100%. It is asking me if I see the flaw and fix it. I honestly dont see anything wrong with the program, can anyone explain to me what is wrong with the program, Thank You.


Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

If you look at the output. What are you seeing and what do you expect to see? What can cause this?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And please refrain from using blue for the whole text. Please use it for highlighting only.
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
I am expecting it to counting down
Ex: 99 bottlles of beer on the wall
99 bottles of beers
take one down.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Look closely at the last lines of the output. What is wrong there?
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
It will be printing out no more bottles of beers on the wall, when the value of beerNum turns 0
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Should it be else if (beerNum = 0 )
System.out.pritln("No more bottles of beer on the wall")
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
alex lesta wrote: . . . else if (beerNum = 0 ) . . .]
No. That won't compile. == 0 might be better, but I haven't looked at the code enough to know.
Prasanna Kumaar
Ranch Hand

Joined: Feb 08, 2011
Posts: 30
following piece of code wont get executed at all

if (beerNum == 1){
word = "bottle"; // singular
}

so even if only one bottle of beer is remaining "1 bottles of beer on the wall" will be printed...'bottles' wont be replaced by 'bottle'

Prasanna Kumaar
Ranch Hand

Joined: Feb 08, 2011
Posts: 30
hope you get it..
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Prasanna Kumaar wrote:following piece of code wont get executed at all

if (beerNum == 1){
word = "bottle"; // singular
}

so even if only one bottle of beer is remaining "1 bottles of beer on the wall" will be printed...'bottles' wont be replaced by 'bottle'





So what should this be
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Prasanna Kumaar wrote:so should this condition be at the end of the program.
if (beerNum == 1){
word = "bottle"; // singular {
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26

So should this condition be at the end of the program. Im new to java

alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Ok I believe I figured it out. line 8 should have been written, look below. Can someone let me know if I'm correct.


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

The best way to tell if you are correct is to compile and run it yourself. That would be faster than waiting for someone else to answer, would be more accurate, and would help you learn.

If it DOESN'T work, then come back, post all your code, tell us what happened, and what you expected to happen.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26


Can anyone help with this program. I am reading HeadfirsJava, and iI gattin an ouput that I can t figure out. Can anyone help me.


Carlo Lopez
Greenhorn

Joined: Mar 05, 2011
Posts: 17

I have executed your program, but changing the number of bottles to 2, and changing some of the code to make it more explicit:



the output is:

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
Take one down.
Pass it around
No more bottles of beer on the wall

what you have to understand here is the iteration, that I think you are understanding, it goes in function of the "index" of the iteration, 'beerNum' in this case.
Try to change the value of 'beerNum', or the index you use, when you don't understand the output.


If you want to reach the stars, then divide and conquer
Shine Sharma
Greenhorn

Joined: Mar 05, 2011
Posts: 1
There was no problem running the code.

It showed all the results successfully.

Initially, beerNum is assigned the value 99.
Then a condition is set in the while loop when beerNum is greater than 0, to evaluate if loop when beerNum is equal to 1.
It prints some text in the mid.
Decrements the value 99 stored in the beerNum by 1.
Again checks whether beerNum is greater than 0 in the if condition, prints some text when true, when false it prints some other text.

That was it!





alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Carlo Lopez wrote:I have executed your program, but changing the number of bottles to 2, and changing some of the code to make it more explicit:



the output is:

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
Take one down.
Pass it around
No more bottles of beer on the wall

what you have to understand here is the iteration, that I think you are understanding, it goes in function of the "index" of the iteration, 'beerNum' in this case.
Try to change the value of 'beerNum', or the index you use, when you don't understand the output.




THANKS FOR THE INFO, IT WORKED
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
alex lesta wrote:THANK YOU
Carlo Lopez wrote:I have executed your program, but changing the number of bottles to 2, and changing some of the code to make it more explicit:



the output is:

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
Take one down.
Pass it around
No more bottles of beer on the wall

what you have to understand here is the iteration, that I think you are understanding, it goes in function of the "index" of the iteration, 'beerNum' in this case.
Try to change the value of 'beerNum', or the index you use, when you don't understand the output.




THANKS FOR THE INFO, IT WORKED
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Carlo Lopez: welcome to the Ranch
Carlo Lopez
Greenhorn

Joined: Mar 05, 2011
Posts: 17

Campbell Ritchie wrote:Carlo Lopez: welcome to the Ranch


Thank you!
alex lesta
Greenhorn

Joined: Apr 08, 2010
Posts: 26
Carlo Lopez wrote:Hey carlos, I am reading to books on Java. Head First java and Just java 2. In your opinion are these books good enough for me to learn java.


Thank you!
Carlo Lopez
Greenhorn

Joined: Mar 05, 2011
Posts: 17

alex lesta wrote:
Carlo Lopez wrote:Hey carlos, I am reading to books on Java. Head First java and Just java 2. In your opinion are these books good enough for me to learn java.


Thank you!


I would suggest "Core Java, Volume 1" from Horstman, excellent book.
J Kim
Greenhorn

Joined: Apr 09, 2011
Posts: 2


In > Ln

and for compile, javac filename.java

after that,

java filename

it will working well.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Carlo Lopez wrote:[
I would suggest "Core Java, Volume 1" from Horstman, excellent book.


alex lesta : i strongly recommend that you read the preface of the above book.
Its not for "beginner programmers" or those "beginning java". I am going to read that book a little later, it was not my first book.The HFJ is ok. Other books you can consider are "how to program" by deitel and "absolute java" by savitch.

hth.
good luck.

SCJP 6. Learning more now.
Prasanna Kumaar
Ranch Hand

Joined: Feb 08, 2011
Posts: 30
Change the while loop condition as >= 1, your program will work 100%

public class BeerSong{
public static void main (String[] args){
int beerNum = 99;
String word = "bottles";

while (beerNum >= 1){

if (beerNum == 1){
word = "bottle"; // singular
}
System.out.println( beerNum + " " + word + " of beer on the wall" );
System.out.println( beerNum + " " + word + " of beer" );
System.out.println("Take one down");
System.out.println("Pass it around");
beerNum = beerNum - 1;
if (beerNum > 0){
System.out.println(beerNum + " " + word + " of beer on the wall");
}else {
System.out.println("No More bottles of beer on the wall");
}

}

}
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
. . . and welcome to the Ranch, J Kim
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beersong
 
Similar Threads
BeerSong Java code
head first beersong problem?
Flaw in Program
Java Beginner
Head first Beersong