• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Beersong

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please refrain from using blue for the whole text. Please use it for highlighting only.
 
alex lesta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look closely at the last lines of the output. What is wrong there?
 
alex lesta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should it be else if (beerNum = 0 )
System.out.pritln("No more bottles of beer on the wall")
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hope you get it..
 
alex lesta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
alex lesta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Greenhorn
Posts: 17
Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carlo Lopez: welcome to the Ranch
 
Carlo Lopez
Greenhorn
Posts: 17
Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Carlo Lopez: welcome to the Ranch



Thank you!
 
alex lesta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In > Ln

and for compile, javac filename.java

after that,

java filename

it will working well.
 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch, J Kim
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic