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

naman tiwari
naman tiwari
773 Points

My Example.java isn't running, giving me errors.

here is my code for Example.java

public class Example {

public static void main(String[] args) {
    // Your amazing code goes here...

System.out.println("We are a making a new Pez Dispenser."); PezDispenser dispenser = new PezDispenser("Yoda"); System.out.printf("The dispenser character is %s\n", dispenser.getCharracterName()); 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.dipense()) { System.out.println("chomp!");

   }
  if(dispenser.isEmpty()) {
    System.out.println("Ate all the PEZ!");}
}

dispenser.load(4); dispenser.load(2); while(dispenser.dipense()) { System.out.println("chomp!");

   }

}

whenever i try to run Example.java it gives me this message.

System.out.println("chomp!");
^
Example.java:28: error: illegal start of type
System.out.println("chomp!");
^
Example.java:32: error: class, interface, or enum expected
}

2 Answers

Ryan Zimmerman
Ryan Zimmerman
3,854 Points

should this be tagged with java or javascript?

naman tiwari
naman tiwari
773 Points

i guess java. but i tagged this with java script. My bad.

Hi,

Please try my code and remember to copy class PezDispenser into different file as in video course:

public class Example {

public static void main(String[] args) {

  System.out.println("We are making new Pez Dispenser");

  PezDispenser dispenser = new PezDispenser("Yoda");

  System.out.printf("Character name is %s\n",dispenser.getCharacterName());

  System.out.println(PezDispenser.MAX_PEZ);

  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");
  }

}

}

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;

}

}

Remember to check if every open brackets are being closed in correct order.

naman tiwari
naman tiwari
773 Points

thank you so much Piotr Stefański.