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 Exceptions

Jordan Jim
Jordan Jim
1,421 Points

Old code not working.

public class Test {

    public static void main(String[] args) {

        System.out.println("We are makaing a new 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 currently empty");

        }
        System.out.println("Loading...");
        dispenser.load();
        if (!dispenser.isEmpty()){
            System.out.println("It is no longer empty");
        }

        while (dispenser.dispense()) {
            System.out.println("Nom");
        } 
        if (dispenser.isEmpty()) {
            System.out.println("You ate all the PEZ!");
        }

        dispenser.load(4);
        dispenser.load(2);
        while (dispenser.dispense()) {
            System.out.println("Nom");
        }
        try {
            dispenser.load();
            System.out.println("This will never happen!");
            } catch (IllegalArgumentException iae) {
                System.out.println("Woah there!");
                System.out.printf("The error was: %s\n", iae.getMessage());
            }

So here is my main

public class PezDispenser {

    public static final int MAX_PEZ = 12;

    private int mPezCount;

    private String mCharacterName;

    public PezDispenser(String characterName){
        mCharacterName = characterName;
        mPezCount = 0;
    }

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

    public boolean dispense() {
        boolean wasDispensed = false;
        if (!isEmpty()){
            mPezCount--;
            wasDispensed = true;
        }
        return wasDispensed;
    }

    public void load() {
        load(MAX_PEZ);
    }

    public void load(int pezAmount) {
        int newAmount = mPezCount + pezAmount;
        if (newAmount > MAX_PEZ){
            throw new IllegalArgumentException("Too many PEZ!!!");
        }
    }

    public String getCharacterName(){
        return mCharacterName;
    }

And here is my Class.

After running my loops no longer run. I don't get an error or exception. I just get the "Loading..." as well as "You ate all the PEZ!" then the exception print out.

Not sure what I did. Using Eclipse to run all of this. If that helps.

2 Answers

public void load (int pezAmount) {
          int newAmount = mPezCount + pezAmount;
          if (newAmount > MAX_PEZ) {
              throw new IllegalArgumentException("Too many PEZ!!!");
          }
          mPezCount = newAmount;
          }

Hi,

If you can, you should use the Teamtreehouse workspace for Java until you know how to modify the code for Eclipse. mayk