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

Chris Rubio
Chris Rubio
1,643 Points

I cannot get Yum Yum Yum to print , please HELP :(

I cannot get Yum Yum Yum to print , I have looked over my class and still cant find whats different between mine and craigs--here are the screen shots --https://w.trhou.se/mfz9bpd4kn

public class Example {

  public static void main(String[] args) {

    PezDespenser despenser = new PezDespenser("vegeta");

    //String differentCharacter = despenser.swapCharacters("Freeza");

    while(despenser.dispense()){
      System.out.println("yum");
    }
    if(despenser.isEmpty()){
      System.out.println("all gone");
    }

  }

}

class PezDespenser{

  public static final int MAX_PEZ = 12;
  final private String character;
  private int pezCount ;

  // when declaring a constructor you want to name it the same as your CLASS !!!

  public PezDespenser(String character){
    this.character=character;
  }

  public String getCharacter(){
    return character;
  }

  public void fill(){
    pezCount = MAX_PEZ;
  }
  public boolean isEmpty(){
    return pezCount == 0;
  }
    public boolean dispense(){
    boolean isDispensed = false;
    if(!isEmpty()){
       pezCount--;
      isDispensed = true;
    }
      return isDispensed;


}
}

Moderator edited: Markdown added so that code renders properly in the forums.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! First, when I look at Craig's code he has many more lines in Example than what you have. That being said, I took a look at your workspace and the reason your code is never printing "yum", is because you never fill the PezDispenser with pez. There is nothing to munch :smile:

If I alter your code just slightly by adding one line, it starts printing "yum":

PezDespenser despenser = new PezDespenser("vegeta");
despenser.fill();

The first line, you have already in Example.java. I simply added the second line which puts pez in the dispenser.

Hope this helps! :sparkles:

Chris Rubio
Chris Rubio
1,643 Points

Omgoodnesss !! thank you soooo much , i cannot believe i did not see that. I LOVE TREEHOUSE !!

I think you need to overload the method fill by which you can prompt the user for pezCount. then you need to call the dispenze mehod.