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 Method Signatures

David Hinton
David Hinton
3,070 Points

Method Signatures

I keep getting the error

' treehouse:~/workspace$ javac Example.java
./PezDispenser.java:31: error: reached end of file while parsing
}
^ Example.java:19: error: cannot find symbol
while (dispense.dispense()) {
^
symbol: variable dispense
location: class Example Example.java:22: error: cannot find symbol
if (dispense.isEmpty()){
^
symbol: variable dispense
location: class Example
3 errors '

My code

'''public class Example {

public static void main(String[] args) {
    // Your amazing code goes here...
  System.out.println("We are making a pez dispenser."); 
  PezDispenser dispenser = new PezDispenser("test");
  System.out.printf("The dispenser Character is %s\n",
                    dispenser.getCharacterName());

if (dispenser.isEmpty()){ System.out.println("The Dispenser is empty"); } System.out.println("Loading..."); dispenser.load();

  if (!dispenser.isEmpty()){
    System.out.println("It is no longer empty");}

while (dispense.dispense()) { System.out.println("Chomp!"); } if (dispense.isEmpty()){ System.out.println("You ate all the pez"); } }
} '''

5 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

In the while loop, dispense.dispense() should be dispenser.dispense(). Also, in your last if statement, dispense.isEmpty() should be dispenser.isEmpty().

David Hinton
David Hinton
3,070 Points

Thank you can't believe I missed that! I changed it but it still gives me 1 error of :

./PezDispenser.java:31: error: reached end of file while parsing
}
^
1 error
treehouse:~/workspace$

Kourosh Raeen
Kourosh Raeen
23,733 Points

Make sure you have the right number of closing braces: }

If you still get that error post your code please. Put three backticks, `, followed by java as the line right before the first line of code. Then put three backticks on its own line right after the last line of code.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey David.

Your code seems fine and does compile for me with no errors. I'm not sure why you are receiving an error. Weird.

My only suggestion is to select all the code in your work space and delete it. Make sure all white space is deleted (If this is the only code you have in the particular file in workspaces, just delete the file and make a new one). Then copy and paste the code you posted above back into the workspace and see if that loses the error. It may just be a glitch in the Workspace.

I'm not sure, but everything you have seems to work fine.

:dizzy:

David Hinton
David Hinton
3,070 Points
public class Example {

    public static void main(String[] args) {
        // Your amazing code goes here...
      System.out.println("We are making a pez dispenser."); 
      PezDispenser dispenser = new PezDispenser("test");
      System.out.printf("The dispenser Character is %s\n",
                        dispenser.getCharacterName());

  if (dispenser.isEmpty()){
    System.out.println("The Dispenser is 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("You ate all the pez");
  }
    }
}
Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi David - Did you get it to work? I just tried the code you pasted above and it compiled and ran fine. Let me know if you're still getting an error.

David Hinton
David Hinton
3,070 Points

treehouse:~/workspace$ javac Example.java && java Example
./PezDispenser.java:31: error: reached end of file while parsing
}
^
1 error

Damn still says it

David Hinton
David Hinton
3,070 Points

Hey guys thank you for you time and answers I fixed the problem, I felt that silly about it that I wasn't going to post but I will just in case others happen to have a brain fart and do the same. I was working on the Example file but the error was in the Dispenser file and wouldn't run because of that.