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 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;
}