qbush
qbush

Reputation: 720

syntax error on token "-", -- expected

I am using Eclipse IDE for Java Developers, Kepler Service Release 2, with jdk1.8.0 installed as the JRE.

I am using Java All-in-one For Dummies to learn Java SE 8, and, when writing a program, using Swing, I am getting this error: syntax error on token "-", -- expected

With this code:

button1 = new JButton("Click me!");
button1.addActionListener(e -> button1Click() );
panel1.add(button1);

exitButton = new JButton("Exit");
exitButton.addActionListener(e -> exitButtonClick() );
panel1.add(exitButton);
this.add(panel1);

button1 and exitButton have been declared as JButtons already, and I have imported javax.swing.* and java.awt.event.*.

How can I fix this?

Upvotes: 4

Views: 3709

Answers (2)

Mew
Mew

Reputation: 415

Leaving this here, since I got here from Google and fixed my own error later on: the Syntax error on token "-", -- expected error is also raised when, instead of using -> in a lambda expression, you use - > (that is, with a space in between the two characters, in which case Java suspects that you're trying to use --).

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240928

if you are on kepler install this update : http://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler

and then set

windows > preferences > java > compiler > compiler compliance level > 1.8 

Upvotes: 16

Related Questions