Reputation: 67
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.
Upvotes: 0
Views: 56
Reputation: 6437
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.
Upvotes: 3