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

Well something is broken...

When trying to compile in the console, I get this error:

Example.java:6: error: constructor PezDispenser in class PezDispenser cannot be applied to given 
types;                                                                                           
        PezDispenser dispenser = new PezDispenser();                                             
                                 ^                                                               
  required: String                                                                               
  found: no arguments                                                                            
  reason: actual and formal argument lists differ in length                                      
1 error

My code is as follows:

// Example.java

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();
        System.out.printf("The dispenser character is %s\n",
                   dispenser.GetCharacterName());
    }
}
//PezDispenser.java

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 isEmpty() {
   return mPezCount == 0;
  }

  public void load() {
    mPezCount = MAX_PEZ;
  }

  public String GetCharacterName() {
    return mCharacterName;
  }

}

Any assistance would be appreciated :)

Thanks!

It does seem to be working in REPL, though. The console is as follows:

Welcome to JavaREPL version 272 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20)               
Type expression to evaluate, :help for more options or press tab to auto-complete.               
java> :load PezDispenser.java                                                                    
Loaded source file from PezDispenser.java                                                        
java> PezDispenser pd = new PezDispenser("William");                                             
PezDispenser pd = PezDispenser@27866406                                                          
java> pd.isEmpty();                                                                              
java.lang.Boolean res1 = true                                                                    

1 Answer

Milo Winningham
seal-mask
.a{fill-rule:evenodd;}techdegree
Milo Winningham
Web Development Techdegree Student 3,317 Points

In Example.java you're missing the characterName argument when creating the new PezDispenser. Your REPL example works because you provide "William" to the constructor.

So it's not really "broken", it just hasn't been defined yet?

Milo Winningham
seal-mask
.a{fill-rule:evenodd;}techdegree
Milo Winningham
Web Development Techdegree Student 3,317 Points

Nope, you do need to fix your code in Example.java since the PezDispenser class is defined as requiring a characterName when it is instantiated.

The error message is a little cryptic but it says essentially the same thing: new PezDispenser() won't work because a String argument is required. In the REPL, new PezDispenser("William") works because you've provided a String argument of "William".

So it has to have a default value defined, then? I tried doing it that way (since in the video Craig has "Yoda" defined as a default value, at least that's my understanding of it), but I still get errors...

From what I can tell, my code is exactly the way it is in the video (except I have "Default" in place of "Yoda"):

//Example.java

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("Default");
        System.out.printf("The dispenser character is %s\n",
                   dispenser.GetCharacterName());      
      if (dispenser.isEmpty) {
        System.out.println("It is currently empty");
      }
      System.out.println("Loading...");
      dispenser.load();
      if (!dispenser.isEmpty) {
        System.out.println("It is no longer empty! :)"); 
      }
    }
}
//PezDispenser.java

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 isEmpty() {
   return mPezCount == 0;
  }

  public void load() {
    mPezCount = MAX_PEZ;
  }

  public String GetCharacterName() {
    return mCharacterName;
  }

}
//This is the error I am currently getting, when I compile Example.java:

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) {                                                                   
         ^                                                                                       
Example.java:14: error: cannot find symbol                                                       
      if (!dispenser.isEmpty) {                                                                  
                    ^                                                                            
  symbol:   variable isEmpty                                                                     
  location: variable dispenser of type PezDispenser                                              
3 errors

Actually, I think I see what is wrong here: In Craig's Example.java, he has:

if (dispenser.isEmpty())

And in mine, it is just:

if (dispenser.isEmpty)

I fixed that and the console reads as follows when Example.java is compiled:

We are making a new Pez Dispenser.                                                               
The dispenser character is Default                                                               
It is currently empty                                                                            
Loading...                                                                                       
It is no longer empty! :)  

So, that being said, just so I better understand how things work, why does isEmpty need the double-parentheses () after it? (is it because it is functioning as a method of dispenser?)