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 Harnessing the Power of Objects Method Overloading

The "Chomp" in my code continues to loop and I don't seem to understand why that is.

It began here:

"while (dispenser.dispense()) { System.out.println("Chomp!") ; }"

When I attempted to bypass this while loop, the same issue occurred with my other while loop a few lines below this initial "Chomp!" while loop.

I don't know exactly why this is occurring and would love some insight from anyone willing to point me in the right direction and attempting to understand where my hiccup occurred.

PezDispenser.java
class PezDispenser {
  public static final int MAX_PEZ = 12;
  final private String characterName;
  private int pezCount;

  public PezDispenser(String characterName) {
    this.characterName = characterName;
    pezCount = 0;
}

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

  public boolean isEmpty() {
    return MAX_PEZ == 0;
  }

  public String getCharacterName() {
    return characterName; 
  }

  public void fill() {  
    fill(MAX_PEZ); 
  }

  public void fill(int pezAmount) {
    pezCount += pezAmount;

  }

}
Example.java
public class Example {

  public static void main(String[] args) {
    // Your amazing code goes here...
    System.out.println("We are making a new PEZ dispenser");
    System.out.printf("FUN FACT: There are %d PEZ allowed in every dispenser %n", 
                      PezDispenser.MAX_PEZ);
    PezDispenser dispenser = new PezDispenser("Yoda");
    System.out.printf("The dispenser is %s %n",
                        dispenser.getCharacterName());
    if (dispenser.isEmpty()) {
      System.out.println("Dispenser is Empty");
    }

    System.out.println("Filling the dispenser with delicious PEZ...");
    dispenser.fill();

    if (!dispenser.isEmpty()) {
      System.out.println("Dispenser is Full");
    }

    while (dispenser.dispense()) {
      System.out.println("Chomp!") ;
    }

    if (dispenser.isEmpty()) {
      System.out.println("Ate all the PEZ");
    }

  dispenser.fill(4); 
  dispenser.fill(2);
    while(dispenser.dispense()) {
      System.out.println("Chomp!!");


    }
  }
}

I posted the script to hopefully make it easier to point out any blatant issues. Thank you in advance.

1 Answer

Michael Hulet
Michael Hulet
47,912 Points

The issue here comes down to your implementation of PezDispenser.isEmpty. To understand this, let's walk through what happens on a single iteration of your loop:

  1. The loop calls dispenser.dispense() to see if it should do another iteration
  2. The dispense method makes a variable called wasDispensed and sets it to false
  3. The if block checks the value of isEmpty() and runs its block if it returns false (normally it would be if it was true, but the ! inverts the value)
  4. isEmpty checks to see if MAX_PEZ is equal to 0, and returns true if it is, and false if it isn't
  5. Since isEmpty returns false, the if block runs
  6. The value of pezCount is decreased by 1
  7. The value of wasDispensed is set to true
  8. dispense returns the value of wasDispensed, which was just set to true
  9. Since dispenser.dispense() returned true, the loop runs

The issue here comes at step 4 in this list. MAX_PEZ is a constant that never changes, and it's set to 12. Since 12 is not equal to 0, isEmpty() will always return false, which means the if block in dispense will always run, and wasDispensed will always be set to true, which means dispense will always return true, so the loop will always run

Based on the context, I think you probably wanted to check to see if pezCount is equal to 0 in isEmpty instead of MAX_PEZ. That way, it starts out with however many it has been filled to have, and the loop will have it count down to 0, which will make isEmpty eventually return true when the PEZ dispenser is empty, and the loop will end

Thank you very much, that was what I intended to do (check if pexCount == 0,)!