0

I'm writing a code to output the "99 Bottles of Beer on the Wall" program. I'm trying to make it say "1 bottle of beer on the wall." instead of bottles. I'm not sure what is wrong with my code. Any help would be appreciated.

public class BeerOnTheWall {
public static void handleCountdown() {
 int amount = 99;
 int newamt = amount - 1;
 String bottles = " bottles";
 while(amount != 0) {
 if(amount == 1) {
 bottles.replace("bottles", "bottle");
 }
 System.out.println(amount + bottles +" of beer on the wall, "
 + amount + bottles +" of beer! You take one down, pass it around, "
 + newamt + " bottles of beer on the wall!");
 amount--;
 newamt--;
 }
 System.out.println("Whew! Done!");
}
public static void main(String args[]) {
 handleCountdown(); 
}
}

I have an if statement that is suppose to check if the int "amount" is equal to one, then replace "bottles" with "bottle".

Any help?

Thank you.

asked Sep 5, 2014 at 3:24

1 Answer 1

3

String#replace returns the modified String, so you need to replace:

if(amount == 1) {
 bottles.replace("bottles", "bottle");
}

with:

if(amount == 1) {
 bottles = bottles.replace("bottles", "bottle");
}

See the documentation.

answered Sep 5, 2014 at 3:26
Sign up to request clarification or add additional context in comments.

1 Comment

I can't believe I missed that! What a simple mistake! Thank you!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.