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 Method Signatures

Pez not dispensing 6 just 12

For the life of me i cannot get the pez to dispense the 6 just the 12. Ive gone through the code and have checked for errors. Any help could be appreciated.

public class Example {
    public static void main(String[] args) {
        // Your amazing code goes here...
      System.out.println("We are making a Pez Dispenser.");
      PezDispenser dispenser = new PezDispenser("Yoda");
      System.out.printf("The dispenser character is %s\n",
                        dispenser.getCharacterName());
      if (dispenser.isEmpty()){
       System.out.println("It is curently empty"); 
      }
      System.out.println("loading...");
      dispenser.load();
      if (!dispenser.isEmpty()){
       System.out.println("It is no longer empty");
      }

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

      dispenser.load(4);
      dispenser.load(2);
      while (dispenser.dispense()){
       System.out.println("Chomp!"); 
      }
    }
}
public class PezDispenser {
  public static final int MAX_PEZ = 12;
  private String mCharacterName;
  private int mPezCount;

  public PezDispenser(String CharacterName) {
   mCharacterName = CharacterName; 
    mPezCount = 0;
  }
  public boolean dispense() {
   boolean wasDispensed = false;
    if(!isEmpty()){
      mPezCount--;
      wasDispensed = true;
    }
    return wasDispensed;  
  }

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

  public void load(int pezAmount) {
    mPezCount += pezAmount;
  }

  public String getCharacterName(){
   return mCharacterName; 
  }
}
Simon Coates
Simon Coates
28,694 Points

are you recompiling prior to execution? (you'd be surprised how many people forget)

i cant get the code to paste on here correctly

Simon Coates
Simon Coates
28,694 Points

enclose with three backticks on line before code and on line after code.

thank you!! here is my code

Thats the thing ive recomplied checked my code through the teacher code and i dunno something isnt clicking in my brain

7 Answers

Simon Coates
Simon Coates
28,694 Points

I think you're telling it to load 18 pez. I'm getting 18 chomps back. So as best i can tell, it's doing exactly what you told it to do.

on the video it shows him doing the same thing and he has a different result.

Simon Coates
Simon Coates
28,694 Points

he empties it after load.

explain please

Simon Coates
Simon Coates
28,694 Points

he runs the load. then does the 12 chomps. He then adds 6 using load and gets 6 more chomps. 18 total.

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

I think your while statement is in wrong place...do you have another file besides these files?

no..... this is frustrating me.

Simon Coates
Simon Coates
28,694 Points

I think you want:

public class Example {
    public static void main(String[] args) {
        // Your amazing code goes here...
      System.out.println("We are making a Pez Dispenser.");
      PezDispenser dispenser = new PezDispenser("Yoda");
      System.out.printf("The dispenser character is %s\n",
                        dispenser.getCharacterName());
      if (dispenser.isEmpty()){
       System.out.println("It is curently empty"); 
      }
      System.out.println("loading...");
      dispenser.load();
      if (!dispenser.isEmpty()){
       System.out.println("It is no longer empty");
      }
      while (dispenser.dispense()){
       System.out.println("Chomp!"); 
      }
      if (dispenser.isEmpty()) {
        System.out.println("Ate all the PEZ!");
      }

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

oh ok that makes sense now!! so, basically im loading one then loading the other i presume?

Simon Coates
Simon Coates
28,694 Points

load 12, dispense, reload 6. dispense. The missing while loop is also why you didn't get the message "Ate all the Pez".

now i get it!! thanks!!