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 trialChris Bensen
2,835 PointsPez 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;
}
}
Chris Bensen
2,835 Pointsi cant get the code to paste on here correctly
Simon Coates
28,694 Pointsenclose with three backticks on line before code and on line after code.
Chris Bensen
2,835 Pointsthank you!! here is my code
Chris Bensen
2,835 PointsThats the thing ive recomplied checked my code through the teacher code and i dunno something isnt clicking in my brain
7 Answers
Simon Coates
28,694 PointsI 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.
Chris Bensen
2,835 Pointson the video it shows him doing the same thing and he has a different result.
Simon Coates
28,694 Pointshe empties it after load.
Chris Bensen
2,835 Pointsexplain please
Simon Coates
28,694 Pointshe runs the load. then does the 12 chomps. He then adds 6 using load and gets 6 more chomps. 18 total.
Cindy Lea
Courses Plus Student 6,497 PointsI think your while statement is in wrong place...do you have another file besides these files?
Chris Bensen
2,835 Pointsno..... this is frustrating me.
Simon Coates
28,694 PointsI 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!");
}
}
}
Chris Bensen
2,835 Pointsoh ok that makes sense now!! so, basically im loading one then loading the other i presume?
Simon Coates
28,694 Pointsload 12, dispense, reload 6. dispense. The missing while loop is also why you didn't get the message "Ate all the Pez".
Chris Bensen
2,835 Pointsnow i get it!! thanks!!
Simon Coates
28,694 PointsSimon Coates
28,694 Pointsare you recompiling prior to execution? (you'd be surprised how many people forget)