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 trialPeter Taylor
Courses Plus Student 4,275 PointsJava - PezDispenser (can't find symbol)
I am getting 2 errors where compiler cannot find symbol (see below). This is the PezDispenser program, and I have provided compiler error and the PezDispenser code.
QUESTION 1) Example calls out a constructor called dispenser, and this allows us to access methods and information within PezDispenser class. There is a public boolean isEmpty that is called out. Why can't the compiler find it??
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
Example.java:9: error: cannot find symbol
if(dispenser.isEmpty){
^
symbol: variable isEmpty
location: variable dispenser of type PezDispenser
Example.java:9: error: illegal start of type
if(dispenser.isEmpty){
^
2 errors
EXAMPLE
public class Example {
public static void main(String[] args) { // Your amazing code goes here... System.out.println("We are making a new Pez Dispenser."); PezDispenser dispenser = new PezDispenser("Donatello"); // need this line explained System.out.printf("The dispenser character is %s\n", dispenser.getCharacterName()); if(dispenser.isEmpty){ System.out.println("Your dispenser is empty");
}
PEZ DISPENSER
public class PezDispenser{//access level modifier
public static final int MAX_PEZ = 12;
private int mPezCount;
private String mCharacterName; //member variable 'm'
public PezDispenser (String characterName){ mCharacterName = characterName; mPezCount = 0; }
public boolean dispense(){ boolean wasDispensed = false; if(!isEmpty()){ mPezCount--; wasDispensed = true; } } public boolean isEmpty(){ return mPezCount == 0; }
public void load(){ mPezCount = MAX_PEZ; } public String getCharacterName(){ return mCharacterName; } }
1 Answer
Seth Kroger
56,413 PointsBecause isEmpty is a method you need to use you have to use parentheses to call the method:
if(dispenser.isEmpty()) {
(You'll also run into an error because you're forgetting to return anything from dispense().)