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.
1 Answer 1
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
AntonH
6,4474 gold badges32 silver badges40 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
John
I can't believe I missed that! What a simple mistake! Thank you!
lang-java