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 Helper Methods and Conditionals

Arjun Vemperala
Arjun Vemperala
1,212 Points

Helper methods and conditionals video

Hi there i am getting some errors in my workspace even though i am exactly following as per instructions in the video

when i type this code in repl PezDispenser pd = new PezDispenser("Yoda");

i am getting either one of these 2 errors
1.Error: cannot find symbol symbol: class PezDispenser location: interface Evaluation PezDispensr method$doepy29rc8sfxi4vk6ln();

  1. java.util.NoSuchElementException

i have attached a snapshot of the worksheet thanks in advance

https://w.trhou.se/5q4ybvhdzn

3 Answers

Simon Coates
Simon Coates
28,694 Points
public class PezDispenser{
  public static final int MAX_PEZ = 12;
  public String mCharacterName;
  private int mPezCount;

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

  public Boolean isEmpty(){
    return mPezCount ==0;
  }

  public void load(){
    mPezCount = MAX_PEZ;
  }
  public String getCharacterName(){
    return mCharacterName;
  }
}

and

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("Yoda");
      System.out.printf("The Dispenser character is %s\n",
                        dispenser.mCharacterName);
    }
}

produces

treehouse:~/workspace$ javac PezDispenser.java                                                           
treehouse:~/workspace$ javac Example.java                                              
treehouse:~/workspace$ java  Example                                                   
We are making a new  Pez Dispenser.                                                    
The Dispenser character is Yoda                                                        
treehouse:~/workspace$ 
Simon Coates
Simon Coates
28,694 Points

so based on what treehouse gave me, you were missing the } to close the class, and had a duplicate method in PezDispenser. I didn't get the exception you seemed to get.

Arjun Vemperala
Arjun Vemperala
1,212 Points

got it, i launched a new worksheet and the code worked just fine your explanation also helped me a lot and most of all i learnt from my mistakes thanks a lot man.

Arjun Vemperala
Arjun Vemperala
1,212 Points

checked the code and even copy pasted your code but still getting the same error

Simon Coates
Simon Coates
28,694 Points

you are recompiling your java after your change it? After you recompile with the above code, you should be able to enter

java Example

into the console. I deliberately included the java and javac commands with my earlier answer.

Arjun Vemperala
Arjun Vemperala
1,212 Points

i am doing the 2 steps not sure if its recompiling 1):load PezDispenser.java Loaded source file from PezDispenser.java

2) PezDispenser pd = new PezDispenser("Yoda");

ERROR: cannot find symbol symbol: class PezDispenser location: interface Evaluation