• 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

head first java beer song program help

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i did the code and it runs ok but the book says there is a flaw in the code, i have tried but cant spot it.


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

while (beerNum > 0) {

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

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");
}
}
}
}

 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new. What do you mean when you say, ". . . the book says there is a flaw in the code?"

What's "the book?" Is it any more specific than that?

And it seems your program could be in an undesirable state. When the loop starts with 2 bottles and transitions to one bottle, the final verse will still use the plural "bottles" rather than the singular "bottle."

Thanks.
 
neil harper
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the book is head first java, it was highly recommended.

here is the output: http://pastebin.com/Ua8egZRa

EDIT:

Greg Brannon wrote:I'm new. What do you mean when you say, ". . . the book says there is a flaw in the code?"

What's "the book?" Is it any more specific than that?

And it seems your program could be in an undesirable state. When the loop starts with 2 bottles and transitions to one bottle, the final verse will still use the plural "bottles" rather than the singular "bottle."

Thanks.


maybe thats the flaw the book speaks of?
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, I see "1 bottles." You caught me in an edit.
 
neil harper
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm, why does it do that? i thought that:
if (beerNum == 1) {
word = "bottle"; //singular as in ONE bottle
}

would prevent that from happening
 
Greg Brannon
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You check for beerNum = 1 when the loop is entered, but beerNum is decremented after the check. Maybe you need another check?
 
neil harper
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh i see so simple moving the if statement down to just after the decrement fixes the issue.

thanks.
 
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic