Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Kirk Yale
Kirk Yale
2,194 Points

trying to change the plural to singular and then back in output

Below is a practice application I'm playing with. Not sure why the output is not showing the correct plural/singular. Can anyone help?

public class PopSong2 { public static void main(String[] args) { int cans = 99; while (cans >= 1) { System.out.printf("%d cans of pop in the cooler. %d cans of pop. %n", cans, cans); if (cans == 1) { System.out.printf("%d can of pop in the cooler. %d can of pop. %n", cans, cans); } cans = cans - 1; System.out.printf("Take one down and pass it around. %d cans of pop in the cooler. %n", cans); } System.out.printf("There are no more cans of pop left in the cooler! :( %n"); System.out.printf("You drank them all! %n"); System.out.printf("Now, You can stop singing. %n"); } }

3 Answers

Hey Kirk you're really close you just need to think about the flow of if cans > 1 then do this also don't forget to format your code properly with mark up. I added some comments in the code snippet below. Basically inside the while loop we need to see if cans is greater or > than 1, then print plural , else print singular

public class Main {
    public static void main(String[] args) {
        int cans = 10;
        while (cans > 0) {
            if (cans > 1) { // here we're checking if cans 10 - 2 is greater than one than print plural
                System.out.printf("%d cans of pop in the cooler. %d cans of pop. %n", cans, cans);
            } else  { // else print singular
                System.out.printf("%d can of pop in the cooler. %d can of pop. %n", cans, cans);
            }
            cans = cans - 1;
            if(cans > 1 ){ // we have to add the same conditional here for these sentences as well
                System.out.printf("Take one down and pass it around. %d cans of pop in the cooler. %n", cans);
            } else {
                System.out.printf("Take one down and pass it around. %d can of pop in the cooler. %n", cans);
            }
        }
        System.out.printf("There are no more cans of pop left in the cooler! :( %n");
        System.out.printf("You drank them all! %n");
        System.out.printf("Now, You can stop singing. %n");
    }
}
Kirk Yale
Kirk Yale
2,194 Points

Hi, Fernando! Thanks for the help. I am still getting the wrong output when I run this code. It doesn't look like it ever executes the else. I am sure this is all newbie mistakes and I'm not far along enough in my studies to understand this, but I'm wondering if I'm still missing something, or if I am trying to make the sequence of words print something it can't since the int changes before the next loop iteration... here's another try:

public class PopSong4 { public static void main(String[] args) { int cans = 10; String word; while (cans > 0) { if (cans > 1) { word = "cans"; } else { word = "can"; //singular } System.out.printf("%d %s of pop in the cooler. %d %s of pop. %n", cans, word); cans = cans -1; System.out.printf("Take one down and pass it around, %d %s of pop in the cooler. %n", cans, word); } System.out.printf("There are no more cans of pop left in the cooler! :( %n"); System.out.printf("You drank them all! %n"); System.out.printf("Now you can stop singing. %n"); } }

Output:

4 cans of pop in the cooler. 4 cans of pop. Take one down and pass it around. 3 cans of pop in the cooler 3 cans of pop in the cooler. 3 cans of pop. Take one down and pass it around. 2 cans of pop in the cooler 2 cans of pop in the cooler. 2 cans of pop. Take one down and pass it around. 1 cans of pop in the cooler 1 cans of pop in the cooler. 1 cans of pop. Take one down and pass it around. 0 cans of pop in the cooler There are no more cans of pop in the cooler! :( You drank them all! Now, you can stop singing.

Process finished with exit code 0

I want it to say this: ...2 cans of pop in the cooler. 2 cans of pop. Take one down and pass it around. 1 can of pop in the cooler 1 can of pop in the cooler. 1 can of pop. Take one down and pass it around. 0 cans of pop in the cooler.

Thanks for any help you can give. Call me persistent :)

have you tried the code snippet i pasted. It only runs the singular case of can when it is referring to one can.

Kirk Yale
Kirk Yale
2,194 Points

It turns out that I was not selecting the correct class application in IntelliJ IDEA, so I was compiling the wrong code the whole time. Once I figured that out and selected the correct class application (your code), it gave the desired output :) Thank you!

Kirk

Kirk Yale
Kirk Yale
2,194 Points

Hi, Fernando! When I execute the code snippet you provided in IntelliJ IDEA is has the following output:

3 cans of pop in the cooler. 3 cans of pop. Take one down and pass it around. 2 cans of pop in the cooler 2 cans of pop in the cooler. 2 cans of pop. Take one down and pass it around. 1 cans of pop in the cooler <--it appears the single can is still printing as a plural) 1 cans of pop in the cooler. 1 cans of pop. Take one down and pass it around. 0 cans of pop in the cooler There are no more cans of pop in the cooler! :( You drank them all! Now, you can stop singing.

Process finished with exit code 0

I appreciate your help with this.

Kirk

Kirk Yale
Kirk Yale
2,194 Points

Here's the final program

public class PopSong6 {
    public static void main(String[] args) {
        int cans = 10;
        while (cans > 0) {
            if (cans > 1) { // here we're checking if cans 10 - 2 is greater than one than print plural
                System.out.printf("%d cans of pop in the cooler. %d cans of pop. %n", cans, cans);
            } else  { // else print singular
                System.out.printf("%d can of pop in the cooler. %d can of pop. %n", cans, cans);
            }
            cans = cans - 1;
            if(cans > 1 || cans == 0){ // we have to add the same conditional here for these sentences as well
                System.out.printf("Take one down and pass it around. %d cans of pop in the cooler. %n", cans);
            } else {
                System.out.printf("Take one down and pass it around. %d can of pop in the cooler. %n", cans);
            }
        }
        System.out.printf("There are no more cans of pop left in the cooler! :( %n");
        System.out.printf("You drank them all! %n");
        System.out.printf("Now, You can stop singing. %n");
    }
}

CHEAA i just ran it and it runs smoothly! Good job man, be sure to keep up the training!