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 trialnaman tiwari
773 PointsMy 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
3,854 Pointsshould this be tagged with java or javascript?
Piotr Stefański
3,080 PointsHi,
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;
}
}
Piotr Stefański
3,080 PointsRemember to check if every open brackets are being closed in correct order.
naman tiwari
773 Pointsthank you so much Piotr Stefański.
naman tiwari
773 Pointsnaman tiwari
773 Pointsi guess java. but i tagged this with java script. My bad.