• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beersong

 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the output. What are you seeing and what do you expect to see? What can cause this?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please refrain from using blue for the whole text. Please use it for highlighting only.
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look closely at the last lines of the output. What is wrong there?
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be printing out no more bottles of beers on the wall, when the value of beerNum turns 0
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should it be else if (beerNum = 0 )
System.out.pritln("No more bottles of beer on the wall")
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hope you get it..
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Kumaar wrote:so should this condition be at the end of the program.
if (beerNum == 1){
word = "bottle"; // singular {
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 17
Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Shine Sharma
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carlo Lopez: welcome to the Ranch
 
Carlo Lopez
Greenhorn
Posts: 17
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Carlo Lopez: welcome to the Ranch


Thank you!
 
alex lesta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In > Ln

and for compile, javac filename.java

after that,

java filename

it will working well.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prasanna Kumaar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch, J Kim
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic