• Post Reply Bookmark Topic Watch Topic
  • New Topic

example 'beersong' java program just hangs  RSS feed

 
Tim Dunphy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I copied a simple program that prints the words from the 'beer song' children's song. I got it from the head first java book.

The program compiles just fine:



But when I go to run the program it just sits there and hangs forever:



I could leave it there all night and it won't do a thing!

Here's the program as it is now:



Can someone help me figure out why this is behaving this way, and how to correct the problem?

Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,
Welcome to CodeRanch!

Here's a hint: Do you see where you while loop ends? In particular, is it before or after you subtract one from the number of bottles of beer.
 
Tim Dunphy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Thanks! I appreciate your reply. And I'm very excited to start my java programming education! I was able to work out the problem. Here's what I got.




And that seems to give the correct result!



Thanks for the tip!

Tim
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job! You've probably noticed that we like to "nudge" towards a solution rather than flat out providing one. You learn more that way. Which of course is the main goal.
 
Tim Dunphy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Good job! You've probably noticed that we like to "nudge" towards a solution rather than flat out providing one. You learn more that way. Which of course is the main goal.


Excellent! Thanks. And yes that's the way I prefer to learn!

Really appreciate the 'nudge'. And it seems like the java ranch is a nice place to hang out and learn!

Cheers!
 
Stacey Christiansen
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on this beer song code also. I got mine to work, but both the book and Mr. Dunphy used an If statement along with the While. I know there's more than one way to code a cat, but just wondering if using the If statement is 'better'. Here's my code.



Thanks for your input!

Stace

ps - I'm new to this forum, so if this is 'hijacking the thread' please accept my apologies. Seems kinda like the same topic.....
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags to your post: always use the tags. Doesn't it look better
I also removed some unnecessary blank lines. Your indentation is inconsistent: move to the right after while...{ not before. It may sound like a tiny thing to notice, but when you have loops which you can't understand, consistent indentation makes it so much easier.

Is that code from Head First Java? Which page?
I think the idea of the if is to check whether the beerNum is greater than 1. Otherwise you print
1 bottles of beer on the wall.
You can use an if to change part of the message from bottles to bottle.
 
Stacey Christiansen
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks ALOT better! Thanks for the tip!

Yes, this is from Head First Java, page 14. That makes sense on the If statement.

Thanks!!
Stace
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!