Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

head first java beer song program help

 
neil harper
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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");
}
}
}
}

 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
oh i see so simple moving the if statement down to just after the decrement fixes the issue.

thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic