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

pawkan kenluecha
pawkan kenluecha
26,303 Points

I can't compile java file. And when I tried to declare a object, they said Java.util.NoSuchElementException

This is code for my Exemple.java

public class Example {

public static void main(String[] args) {
    // Your amazing code goes here...
System.out.println(" we're making a new PezDispenser.");
// PezDispenser dispencer = new PezDispenser("Donatello");
// System.out.printf("The dispenser charater is %s",dispencer.getCharacterName());

}

}

I tried to find error by excluding it with //, however with bad luck they still showed me a error message "Error: Could not find or load main class Example.java"

This is class file. I don't sure what is the problem. If anyone can enlighten me. I'll really appreciate.

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

public String getCharacterName(){ return mCharacterName; } }

2 Answers

You have a mis-spelling.

public class PezDispenser {
    public static final int MAX_PEZ = 12;    //MAX_PEZ
    . . .
public void load() { 
    mPezCount = MAX_PEX;                    //MAX_PEX
}
pawkan kenluecha
pawkan kenluecha
26,303 Points

Yes, I tried that out already but I still can't compile the main file (Example.java) They always said "Error: Could not find or load main class Example.java".

This is the Example class. It must be in a file named Example.java:

public class Example {

   public static void main(String[] args) {

      // Your amazing code goes here...
      System.out.println(" we're making a new PezDispenser.");
      PezDispenser dispencer = new PezDispenser("Donatello");
      System.out.printf("The dispenser charater is %s", dispencer.getCharacterName());
   }
}

This is your PezDispenser class. It must be in a file named 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;
   }
}

I've loaded both files into my Java IDE and they run fine. I suggest you delete yours, and create new files with the above, just to shortcut trying to find the differences. Good luck.