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

PezDispenser not empty even though value is set to 0?

Hi

I'm currently on this step of the Java course: https://teamtreehouse.com/library/java-objects/harnessing-the-power-of-objects/helper-methods-and-conditionals I created the isEmpty method and did everything like in the video, but when i call the function using the repl, I get "false" instead of "true". I can't seem to find the error in my code - the variable "mPezCount" is correctly set to 0 in the initializer method...

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 isEmpty() {
    return mPezCount == 0;
  }

  public void load(){
    mPezCount = MAX_PEZ;
  }

  public String getCharacterName() {
    return mCharacterName;
  }
}

Thanks in advance.

Hi Marina, could you please provide the exact lines of code that you're typing in REPL? Thanks ;)

Sure, I'm typing:

Pezdispenser newPD = Pezdispenser("Name");
newPD.isEmpty();

And then it returns false..

But when I run the code in the console (the last part of the video), it seems to be correct. It says the dispenser is empty and then it loads it up. So the repl is the problem I think..

2 Answers

Hi Marina,

Using the same code as yours, I got this in the repl:

java> PezDispenser pd = new PezDispenser("Steve");                                          
PezDispenser pd = PezDispenser@29b6af01                                                     
java> pd.isEmpty();                                                                         
java.lang.Boolean res1 = true 

So your code works fine. That's odd you're getting a different output.

Maybe post your REPL code as Jacek suggested; maybe the issue is in there?

Steve.

In code you've provided you've got PezDispenser spelled wrong (lowercase d instead of uppercase one). Please try typing it with uppercase D and let me know if it's working.

Jacek.

Agree. But I'm not sure the repl would return false in those conditions. It'd throw an unknown identifier error, or such like.

I'd be interested to see what the fix is here!

So I went back to the video just now and tried again - this time it worked... I am pretty sure I spelled everything correct in the compiler, but maybe I missed something. I was just weird since it worked when running the actual program in the console..

Thanks anyway! :-)

That's great you got sorted.

Happy coding!

Steve.