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 trialelineurycollado
631 PointsThe "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.
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;
}
}
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
47,913 PointsThe 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:
- The loop calls
dispenser.dispense()
to see if it should do another iteration - The
dispense
method makes a variable calledwasDispensed
and sets it tofalse
- The
if
block checks the value ofisEmpty()
and runs its block if it returnsfalse
(normally it would be if it wastrue
, but the!
inverts the value) -
isEmpty
checks to see ifMAX_PEZ
is equal to0
, andreturn
strue
if it is, andfalse
if it isn't - Since
isEmpty
returnsfalse
, theif
block runs - The value of
pezCount
is decreased by 1 - The value of
wasDispensed
is set totrue
-
dispense
returns the value ofwasDispensed
, which was just set totrue
- Since
dispenser.dispense()
returnedtrue
, 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 fill
ed 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
elineurycollado
631 Pointselineurycollado
631 PointsThank you very much, that was what I intended to do (check if pexCount == 0,)!