Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Beginner

 
Antonio Nobrega
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, im new to the forum and also to java, loving it so far. Heres my question:

Following an exemple of a book im reading here is the code to a classic song, "99 bottles of beer":




However if i do it like that it prints the "x bottles of beer in the wall" twice.
The book also says that there is one little flaw in the code and asks me to fix it. I changed a couple things and i am not sure if i was supposed to do it. If you see the code above all the "if" and "else" ( sorry dunno how to call these in english haha ) are inside the while {}, i removed them as you can see beyond and it worked fine, was that the flaw? The code above also says "bottles" once when it is supposed to say "bottle". Sorry about my english, its not my main language.




 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

First of all , use code tag , while posting your code .

Coming to your question, Yes there is a repeat line *System.out.println (beerNum + " " + word + " of beer on the wall ");*

just analyse , why it is executing twice *everytime* . and

what about this line *System.out.println("No more bottles of beer on the wall"); * , it is never executed , *why ?*
 
ahamed irshad
Ranch Hand
Posts: 31
C++ Notepad Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notice that there are two (same) system.out.println statements -one within if condition and other before if condition.we will get two tatements in output because of this if condition.it will get executed until beernum=0.so we get two same statement
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Antonio, welcome to JavaRanch.

I'm a moderator on this forum, and I added code tags to your post, so that your source code is easier to read. Please use the code tags yourself next time when you post source code.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second if-statement is only there to make sure you end with "no more bottles of beer on the wall". If you take that print-out outside of the loop it will also be printed at the end. You also know that at this time, the loop has ended so beerNum <= 0 (and since beerNum started > 0, it will even be == 0).
 
Antonio Nobrega
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:Welcome to JavaRanch

First of all , use code tag , while posting your code .

Coming to your question, Yes there is a repeat line *System.out.println (beerNum + " " + word + " of beer on the wall ");*

just analyse , why it is executing twice *everytime* . and

what about this line *System.out.println("No more bottles of beer on the wall"); * , it is never executed , *why ?*


Hey thanks for all the answers, sorry i didnt know about the Code Tag thing.
The line "no more bottles of beer on the wall" is working fine here on both examples, isnt it? I found out why System.out.println (beerNum + " " + word + " of beer on the wall "); was executing twice, thanks to you guys ^^.

Another thing, the first code ( the one saying "...bottles of beer on the wall" twice) when executed prints "1 bottles of beer on the wall" and then "1 bottle of beer on the wall", correct me if i am wrong:




The first if is saying that if beerNum == 1 use the world bottle but the second one also says that if beerNUM =1 ( > 0 ) use "word" and this word is bottles right? Meh sorry if i am not explaining this right.

Thanks to everyone that replied, Java Ranch rocks!
 
Antonio Nobrega
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone?
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct; you start by setting word to "bottles", then when you get to 1, change it to point to "bottle". If you are getting 1 bottle printed twice, you might have the print statement in the wrong place.
 
raj sawant
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic