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 Incrementing and Decrementing

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

I've got chomping on an infinite loop!

I feel like I should know the answer to this but I've looked all over my code.

  • The wasDispensed variable is correctly set to a boolean
  • I'm definitely checking that the dispenser is empty before logging "Chomp" to the console.
  • I can't spot any syntax errors in my code on either file.

To be honest I'm stumped and I can't find what the problem is.

Example.java
public class Example {

    //main class
    public static void main(String[] args) {

        //print message to console.     
        System.out.println("We are making a new Pez Dispenser");

        //instantiate new object
        PezDispenser dispenser = new PezDispenser("Jack");

        //print to the console the returned value from getCharacterName.
        System.out.printf("This dispenser is called %s %n", dispenser.getCharacterName()
                         );



        //check status of PEZ Dispenser
        if (dispenser.isEmpty()) {
          System.out.println("Dispenser is empty");
        }

        //print message to console
        System.out.println("Filling the dispenser with delicious PEZ....");
        //Call the fill method
        dispenser.fill();

        //check status of PEZ Dispenser
        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");
    }


    }

}
PezDispsenser.java
class PezDispenser {

    //declare new constant variable
    final static int MAX_PEZ = 12;

    //private variable accessible only to PezDispenser class    
    final private String characterName;
    private int pezCount;

    //initialise the characterName variable
    public PezDispenser(String characterName) {
        this.characterName = characterName;
        pezCount = 0;
    }

    //Fill Pez Dispenser
    public void fill() {
        pezCount = MAX_PEZ;

    }

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


    //Check status of Pez
    public boolean isEmpty() {
        return MAX_PEZ == 0;

    }

    //getter method. returns characterName
    public String getCharacterName() {
        return characterName;
    }  

}

1 Answer

MAX_PEZ is static and equals 0 and you keep calling isEmpty() which will consistently return true. What needs to be swapped is:

pezCount --> for MAX_PEZ in the isEmpty() method.

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

;)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

got it thanks. I knew it was simple, I just couldn't see it :)