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

Whitney Barber
Whitney Barber
9,453 Points

Java, I cannot get my example class to run

I cannot compile and run my example.java class. When I terminate the repl and enter clear && javac Example.java && java Example I get his message...

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
Example.java:12: error: cannot find symbol
System.println("Loading....");
^
symbol: method println(String)
location: class System
./PezDispenser.java:11: error: missing method body, or declare abstract
public boolean isEmpty(); {
^
./PezDispenser.java:12: error: return outside method
return mPezCount == 0;
^
./PezDispenser.java:19: error: missing method body, or declare abstract
public void load(); {
^
5 errors

Here is my code...

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());
  if (dispenser.isEmpty()) {
    System.out.println("It is currently empty");
  }
  System.println("Loading....");
  dispenser.load();
  if (!dispenser.isEmpty()) {
    System.out.println("It is no longer empty");
  }
}

}

3 Answers

Kumar Bharath H N
Kumar Bharath H N
8,324 Points

Hi Barber,

One of the errors is that your constructor needs the "PezName" as argument as mentioned above by Travis. and for the Loading... printf line println syntax is wrong it should be

 System.out.println("Loading....");

and the next error is with method syntax. You have closed methods with a semicolon and declaring which makes the entire body of the method not compiled!!

so in "public boolean isEmpty();" and "public void load();" methods remove the semicolon!!

Hope this helps :)

Happy coding!!

Travis Avey
Travis Avey
18,876 Points

It looks like your constructor requires a string:

PezDispenser dispenser = new PezDispenser("Pez Dispenser Name");
Whitney Barber
Whitney Barber
9,453 Points

Travis and Kumar, those suggestions got it working, thank you both very much!