0

So I was trying to figure out how this snake game works:

http://zetcode.com/tutorials/javagamestutorial/snake/

When I copied the code into a compiler, I got many errors in this one block of code:

public static void main(String[] args) {

     EventQueue.invokeLater(() -> {
         JFrame ex = new Snake();
         ex.setVisible(true);
     });
  }
}

Here are the errors: (Line 27 is the EventQueue.invokeLater line)

Error: illegal start of expression (Line 27)
Error: illegal start of expression (Line 27)
Error: illegal start of expression (Line 27)
Error: ';' expected (Line 27)
Error: illegal start of type (Line 30)
Error: class, interface, or enum expected (Line 32)
Martin C.
  • 12,140
  • 7
  • 40
  • 52
lefty
  • 25
  • 4

1 Answers1

1

You need to have at least Java 8 for using Lambdas (like you do in this example).

For Java 7, you'll need to resort to resort to using Runnable instead of Lambda

EventQueue.invokeLater(new Runnable() {
    public void run() {
      JFrame ex = new Snake();
      ex.setVisible(true);
    }
});
Martin C.
  • 12,140
  • 7
  • 40
  • 52
  • Oh okay thank you. Is there another way to code this so that it works with java 7? My computer class uses java 7 for some reason – lefty Jun 07 '18 at 17:56
  • @LFTY I added a code-example. But since you are following a tutorial, it's very likely that the rest of the code uses Java 8 features as well. – Martin C. Jun 07 '18 at 17:58
  • Hi again, I didn't know how to contact you so this was the only way. I've tried to create a method to make a replay button pop up when the game was over, but I was not able to do it properly. How would I do this? – lefty Jun 11 '18 at 21:59