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.util.NoSuchElementException

I am getting this error. I saw the answer to the previously asked question but I was not satisfied with the answer. When I try this command after starting java-repl i typed the following commad which is:

PezDispenser pd = new PezDispenser("Yoda"); Thanks in advance

1 Answer

That could because there is no element PezDispenser. According to the documentation:

Thrown by various accessor methods to indicate that the element being requested does not exist.

Your expression should work fine if you defined a class PezDipsenser.

class PezDispenser {
     PezDispenser(String name) {
           /* . . . */
     }
     /* . . . */
}

class PezDispenser { public static final int MAX_PEZ = 12; private String mCharacterName; private String mPezCount;

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

public String getmCharacterName(){ return mCharacterName; }

public void load() { mPezCount = MAX_PEZ; }

}

This code still isn't working? Whats the problem

The error message I get is:

ERROR: incompatible types: int cannot be converted to java.lang.String
    mPezCount = 0;
                ^
ERROR: incomparable types: java.lang.String and int
    return mPezCount == 0;
                     ^
ERROR: incompatible types: int cannot be converted to java.lang.String
    mPezCount = MAX_PEZ;

Does converting mPezCount to an int make it work?

class PezDispenser {
  public static final int MAX_PEZ = 12;
  private String mCharacterName;
  private int mPezCount; // Should be an "int".

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

  public String getmCharacterName(){
   return mCharacterName;
  }

  public void load() {
    mPezCount = MAX_PEZ;
  }  
}