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

What is wrong with my java code please?

public class Pez { public final int MAX_PEZ = 12; private String mCharacterName; private int mPezCount;

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

public boolean isEmpty() { mPezCount == 0;

public void load() { mPezCounr = MAX_PEZ; }

public String getCharacterName() { return mCharacterName; }

} }

This is where Public class Pez is initiated:

public class Example {

public static void main(String[] args) {

    System.out.println("We are making a new Pez Dispenser.");
    Pez dispenser = new Pez("Yoda");
    System.out.printf("The dispenser charecter 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");
    }
}

}

2 Answers

So I rewrote the block of code where I believe there is an error:

public boolean isEmpty() { 
     mPezCount == 0;

    public void load() { 
           mPezCounr = MAX_PEZ; }

public String getCharacterName() { 
        return mCharacterName; }

} }

If you look inside I think the brackets are misaligned. As it looks like functions are being defined inside functions. So it looks like load and getCharacterName are being defined inside the isEmpty function. This is a nested function. And unfortunately Java does not allow nested functions. One thing that really helps is if you align the closing bracket with the function name, like below:

public boolean isEmpty() { 
     mPezCount == 0;
}
 public void load() { 
           mPezCounr = MAX_PEZ; 
}
public String getCharacterName() { 
        return mCharacterName;
 }

let me know if this doesn't work. If it doesn't could you post the stack trace. That would really help in identifying errors.

I see two more small errors - the isEmpty() method is missing the word "return," and there's a misspelled variable name in the load() method.