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 Java Objects (Retired) Harnessing the Power of Objects Incrementing and Decrementing

Question about the While loop.

I'm currently following along with this video series and I got to the part where I put the While loop in. In the video he typed this:

public boolean dispense() {
    boolean wasDispensed = false;
    if (!isEmpty()) {
        mPezCount--;
        wasDispensed = true;
    }
    return wasDispensed;
}

But I typed this:

public boolean dispense() {
    boolean wasDispensed = false;
    if (!isEmpty()) {
        mPezCount =- 1;
        wasDispensed = true;
    }
    return wasDispensed;
}

When I typed it the way I wanted with the =- 1, I got an infinite loop? But when I did it his way it worked. Shouldn't it work the same even though I'm using a different method to achieve the same result?

1 Answer

Armond Luthens
Armond Luthens
1,189 Points

it should be mPezCount -= 1;