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 Harnessing the Power of Objects Abstraction at Play

Dont know where to Im missing a curly bracket

Im missing a curly bracket somewhere not sure on my code. Its my only error. I seem to be having trouble with it heres my code.

 public class Example {  

  public static void main(String[] args) {
    // Your amazing code goes here...
    System.out.println("We are making a new Pez dispenser!");
    System.out.printf("FUN FACT: There are %d PEZ allowed in every dispenser %n",
                      PezDispenser.MAX_PEZ);

    PezDispenser dispenser = new PezDispenser("Donatello");
      System.out.printf("The dispenser is %s \n",
                         dispenser.getCharacterName()
                       );

    if(dispenser.isEmpty(() -> {
    System.out.println("Dispenser is empty");} ))

   System.out.println("Filling the dispenser with gangsta PEZ...");
    dispenser.fill();
  if (!dispenser.isEmpty(() -> {
    System.out.println("dispenser is full");
  }

It pin points to my last bracket being the problem I just dont know how to give a solution for the problem.

Moderator edited: Please refer to the Markdown Cheatsheet link at the bottom of the "Add an answer" section for tips on how to properly format code inside your question.

3 Answers

Oleksiy Slisenko
Oleksiy Slisenko
7,002 Points

Hi !

I'm not guru, but it seems to me that it's not the right case for lambda expression. Actually, I see no advantage to write:

if(dispenser.isEmpty(() -> { System.out.println("Dispenser is empty");} ))

instead of:

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

the last one looks even shorter and, IMHO, more understandable.

Nevertheless, after getting rid of lambdas and adding two closing curly brackets at the end of code, You will get Your example compiled. (I've just checked it out)

public class Example {  

  public static void main(String[] args) {
    // Your amazing code goes here...
    System.out.println("We are making a new Pez dispenser!");
    System.out.printf("FUN FACT: There are %d PEZ allowed in every dispenser %n",
                      PezDispenser.MAX_PEZ);

    PezDispenser dispenser = new PezDispenser("Donatello");
    System.out.printf("The dispenser is %s \n",
                         dispenser.getCharacterName()
                       );
    if(dispenser.isEmpty()) {
    System.out.println("Dispenser is empty");}

    System.out.println("Filling the dispenser with gangsta PEZ...");
    dispenser.fill();
    if (!dispenser.isEmpty()) {
        System.out.println("dispenser is full");
    }
  }
}

P.S. Thanks for question ! It motivated me to look up lambdas in documentation :-)

There must be something else wrong because I cant compile without lambdas and I still get the parsing error even when I add the other curly brackets.

Oleksiy Slisenko
Oleksiy Slisenko
7,002 Points

The code I provide above was compiled by me for sure ! You may copy-paste it and try yourself, but as well You may provide Your new code here (without lambdas and with curly bracket) to see what else is wrong... It's up to You.

I copy and pasted and it is still happening I'm wondering if theres something wrong with my pezdispner code in the other file. Thank you I really appreciate.

Hello Bryan if you still have that problem...did you try to add one more curly bracket at the end?

I did ;/ its so weird.