*
The moose likes Beginning Java and the fly likes Java Beginner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Beginner" Watch "Java Beginner" New topic
Author

Java Beginner

Antonio Nobrega
Greenhorn

Joined: Apr 05, 2010
Posts: 11
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Feb 26, 2010
Posts: 31

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


You can do anything, but not everything
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14164
    
  19

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Antonio Nobrega
Greenhorn

Joined: Apr 05, 2010
Posts: 11
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

Joined: Apr 05, 2010
Posts: 11
Anyone?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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

Joined: Apr 08, 2010
Posts: 3
lol
 
jQuery in Action, 2nd edition
 
subject: Java Beginner