Robert
Robert

Reputation: 4406

I am getting a curly brace error but I do not understand why

I am getting the error java:35: error: illegal character: \29, all this should do is when a user clicks on the label it changes from "H" to "T"

I am also getting error: ';' expected }

and error: reached end of file while parsing }

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class Lab3Label extends JLabel{

    Lab3Label () {
        this.addActionListener(new FlipTheCoinListener());
    }



    public void FlipTheCoin(){
        int count = 0;
        if(count % 1 == 0 ){
            this.setText("H");
            count += 2;
        }
        else{
            this.setText("T");
            count -= 2;
        }
    }


}

        class FlipTheCoinListener implements ActionListener{


            public void actionPerformed(ActionEvent e){
             this.FlipTheCoin();
            }
    }

^ is the line in question

Upvotes: 1

Views: 2872

Answers (3)

driangle
driangle

Reputation: 11779

Based on the code you gave, it doesn't look like there's a 'curly-brace error'.

There are three main things you have to fix in your code:

  1. Define FlipTheCoinListener as an inner class inside Lab3Label.
  2. The method FlipTheCoin is part of the outer class Lab3Label, look into how to call methods of an outer class, from an inner class.
  3. The method addActionListener does not exist for JLabel,find the appropriate listener method you're supposed to use.

Upvotes: 1

pmurph
pmurph

Reputation: 51

This may be an error in the way you formatted your post but you have a } before your import statements.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881563

You have a funny character on that line, as evidenced by the actual error you're getting, complaining about \29. It may be because you cut and pasted the code in from somewhere with the extraneous character.

If you really wanted to investigate properly, you could examine the hex contents of the file such as with (UNIXy systems):

od -xcb sourcefile.java

but it's probably easier just to delete the line totally and retype it.

Upvotes: 5

Related Questions