Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Harnessing the Power of Objects Incrementing and Decrementing

Julius Williams
Julius Williams
2,522 Points

Not sure how to solve these errors

I've followed the videos leading up to this one and everything was going fine, but now I'm getting a litany of errors. I think it's all steming from the isEmpty variable but can't figure out how to fix it.

Here's what I've got in there:

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() { mPezCount = MAX_PEZ; }

public String getCharacterName() { return mCharacterName; } }

And here are the errors I'm getting:

./PezDispenser.java:20: error: illegal start of expression
public boolean isEmpty() {
^
./PezDispenser.java:20: error: ';' expected
public boolean isEmpty() {
^
./PezDispenser.java:24: error: illegal start of expression
public void load() {
^
./PezDispenser.java:24: error: illegal start of expression
public void load() {
^
./PezDispenser.java:24: error: ';' expected
public void load() {
^
./PezDispenser.java:28: error: illegal start of expression
public String getCharacterName() {
^
./PezDispenser.java:28: error: ';' expected
public String getCharacterName() {
^
./PezDispenser.java:31: error: reached end of file while parsing
}
^
Example.java:8: error: cannot find symbol
dispenser.getCharacterName());
^
symbol: method getCharacterName()
location: variable dispenser of type PezDispenser
Example.java:9: error: cannot find symbol
if (dispenser.isEmpty()) {
^
symbol: method isEmpty()
location: variable dispenser of type PezDispenser
Example.java:13: error: cannot find symbol
dispenser.load();
^
symbol: method load()
location: variable dispenser of type PezDispenser Example.java:13: error: cannot find symbol
dispenser.load();
^
symbol: method load()
location: variable dispenser of type PezDispenser
Example.java:15: error: cannot find symbol
if (!dispenser.isEmpty()) {
^
symbol: method isEmpty()
location: variable dispenser of type PezDispenser
Example.java:22: error: cannot find symbol
if (dispenser.isEmpty()) {
^
symbol: method isEmpty()
location: variable dispenser of type PezDispenser
./PezDispenser.java:14: error: cannot find symbol
if (!isEmpty()) {
^
symbol: method isEmpty()
location: class PezDispenser
./PezDispenser.java:29: error: incompatible types: String cannot be converted to boolean
return mCharacterName;
^
15 errors

1 Answer

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

It looks like you forgot to close your if statement

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