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
Krondor The Destroyer
1,556 PointsWhat 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; }
} }
2 Answers
Zuhayr Elahi
2,582 PointsSo 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.
Kathryn Ann
10,071 PointsI see two more small errors - the isEmpty() method is missing the word "return," and there's a misspelled variable name in the load() method.
Krondor The Destroyer
1,556 PointsKrondor The Destroyer
1,556 PointsThis is where Public class Pez is initiated:
public class Example {
}