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 Exceptions

Zachary Martin
Zachary Martin
3,545 Points

Pez, Example. java won't compile. Reached end of file while parsing.

Followed along in the example pez dispenser all the way to the end and I keep getting the error message reached end of file while parsing which is usually fixed by adding a bracket but nothing works.

Zachary Martin
Zachary Martin
3,545 Points

public class Example {

public static void main(String[] args) {
    // Your amazing code goes here...
  System.out.println(" We are making a brand 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.out.println("Loading...");
  dispenser.load();
  if (!dispenser.isEmpty()) {
    System.out.println ("It is no longer empty");
  }
  while (dispenser.dispense()) {
    System.out.println ("Chomp!");
  }

  if (dispenser.isEmpty()) {
    System.out.println ("Ate all the pez");

  }
}
Zachary Martin
Zachary Martin
3,545 Points

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

public PezDispenser (String characterName){
mCharacterName = characterName;

}

public String getCharacterName() { return mCharacterName; mPezCount = 0;

}

public booean dispense() { boolean wasDispensed = false; if (!isEmpty()) { mPezCount--; wasDispensed = true; } return wasDispensed; }

public boolean isEmpty() { return mPezCount ==0;

public void load() { load(MAX_PEZ); } public void load (int pezAmpount) { int newAmount = mPezCount + pezAmount if (newAmount > MAX_PEZ) { throw new IllegalArgumentException ("Too Many Pez") mPezCount = newAmount; }

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

You're missing a curly brace at the end of the file. That error is almost always caused by mistakes in matching curly braces.

Tyler Combs
Tyler Combs
15,525 Points

Tip: You can save a snapshot of your workspace and share the link to it so people can view your complete files in workspace without you having to copy/paste a bunch of code. It should be one of the 3 or so icons in the top right of your workspace. When others are viewing your snapshot they can also edit your code and compile and run the program. (it only saves changes client-side)