• Post Reply Bookmark Topic Watch Topic
  • New Topic

"99 bottles of beer" code  RSS feed

 
Marcatus Zer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I a Java noob and I'm learning with this book: Head Fist Java. I would like some help solving one exercise in the book:



"There’s still one little flaw in our code. It compiles and runs, but the output isn’t 100% perfect. See if you can spot the flaw, and fix it." ?that's the question?

When I ran the code in Eclipse what i could conclude is that the problem is with the spacing phrases; it does not seem to flow like a paragraph. I guess instead of using "println" we should use only "System.out.print"
So what are your suggestions?

Thanks for any help.
 
Popa Mihai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

It may have something to do with the last iteration...
 
Marcatus Zer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for the reply.
Hmm what do you mean by last "iteration"? could you be more specific.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch both of you.
 
Popa Mihai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Maybe the term "iteration" is not used correctly as I am, somehow, a beginner myself. Reformulating (and correcting): Please check the output from the penultimate execution of the while.

Thank you.
 
Popa Mihai
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And welcome to the Ranch both of you.


Thank you. I have been here for a while mostly reading. I am trying now to give something back, if I can, while I keep on learning.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another hint: you've done a good job of getting singular and plural correct, but not perfect.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Popa Mihai wrote:Maybe the term "iteration" is not used correctly

"iteration" is the correct term to use.
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcatus, I have the same book and have been working through it. When I got to this I ran the program to see what they were talking about and it became very apparent after reading the output.

Run the program and see what comes out, then go and fix it, the knowledge of fixing your own "mistake" is invaluable. (at least that is what I did)
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you aren't a native English speaker it might be harder. Since they are saying last iteration already, I will say change the 99 in the code to 2 and watch the output carefully. After you see it, fix it, and change it back to 99 and see if it still works. Let the fix be moving or deleting and adding code, not only adding code.

This is probably a good book because here it is illustrating well the most insidious evil kind of problem - everything works great except in one (or a few) cases. You can easily see it in the output in the example - but out in the field something might work for a long time until that special case is hit and then all hell breaks loose.


 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when beerNum is equal to 0?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!