File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes head first java beer song program help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "head first java beer song program help" Watch "head first java beer song program help" New topic
Author

head first java beer song program help

neil harper
Greenhorn

Joined: Oct 30, 2010
Posts: 14
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

Joined: Oct 24, 2010
Posts: 560
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.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
neil harper
Greenhorn

Joined: Oct 30, 2010
Posts: 14
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

Joined: Oct 24, 2010
Posts: 560
Yup, I see "1 bottles." You caught me in an edit.
neil harper
Greenhorn

Joined: Oct 30, 2010
Posts: 14
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

Joined: Oct 24, 2010
Posts: 560
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

Joined: Oct 30, 2010
Posts: 14
oh i see so simple moving the if statement down to just after the decrement fixes the issue.

thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: head first java beer song program help