简体   繁体   中英

eclipse editor expecting me to start and end a block

I would really appreciate some help on what seems to be an easy error to fix, I'm a beginner and have been stuck on this for 2 hours.

Eclipse gives me two errors that expect me to start and end a random block. In the code I give, I comment on the two lines where there are errors. This is what those errors say:

1.Syntax error on token ";", { expected after this token

2.Syntax error, insert "}" to complete Block

This is the code from my unfinished class (I comment where the two errors are):

    //import java.awt.*;
    import java.awt.event.*;
    //import java.util.Collection;
    //import java.util.LinkedList;
    import java.util.*;
    import javax.swing.*;

        public class Controller implements ActionListener
        {           //private Model model;
        private View view;

        //create linked list to hold employees
        Collection <Employee> c; //ERROR ON THIS LINE
        c = new LinkedList <Employee>();

        //create a few employees to use
        Employee emp; 
        emp = new Employee("Hugh Jackman", "609-66-9598", 90000.00, Employee.maritalStatus.single);
        Manager man;
        man = new Manager("Software Manager", 100.00, "John Smith", "678-57-1295",
                120000.00, Employee.maritalStatus.married);
        Worker wor;
        wor = new Worker("Hugo Boss", "Clothing", "Andrew Park", "534-47-9876",
                15000, Employee.maritalStatus.partner); //ERROR ON THIS LINE



        public Controller()
        {            
            view = new View(this);
                    //model = new Model();
        }

        public static void main(String args[])
        {            

            // instantiates itself to get its own constructor going
                    Controller ctr = new Controller();
        }

        public void actionPerformed (ActionEvent evt)
        {           
            //System.out.println((JMenuItem)evt.getSource());
            if (((AbstractButton) evt.getSource()).getText() == "Employee")
            {   
                c.add(emp);
                view.show("Employee added!");
            }

        }
}

write this statements in one line

Collection <Employee> c = new LinkedList <Employee>();

Employee emp = new Employee("Hugh Jackman", "609-66-9598", 90000.00, Employee.maritalStatus.single);
Manager man = new Manager("Software Manager", 100.00, "John Smith", "678-57-1295",120000.00, Employee.maritalStatus.married);
Worker wor = new Worker("Hugo Boss", "Clothing", "Andrew Park", "534-47-9876",15000, Employee.maritalStatus.partner);

as you initializing the object after declare the object. This are the executable statement which only allow within method only while the above is the different thing it will declare the object with the initialization so the compiler consider as one statement not 2 statement

Collection <Employee> c;
c = new LinkedList <Employee>();

this are the two statement.

You seem to be adding executable statements in class declaration.

 //create linked list to hold employees
 Collection <Employee> c; //ERROR ON THIS LINE
 c = new LinkedList <Employee>();

You should do it in constructor or some method. You can only declare fields and initialize them here.

Not directly related to your problem, but for your string comparison you'll want to use the String.equals() method instead of the == operator.

Instead of

if (string1 == string2) { ... }

use

if (string1.equals(string2)) { ... }

Strings are first-class objects in Java, and so you need to compare them as first-class objects. Comparing strings using == is defined, but may have unexpected results.

You say you are using Eclipse, you should pay attention to the wealth of information it gives you about the errors in your code.

Your first error is the improper initialization of your class variable. Change:

Collection <Employee> c; //ERROR ON THIS LINE
c = new LinkedList <Employee>();

To:

Collection <Employee> c = new LinkedList <Employee>();

Then correct each of the subsequent initializers in the same way ( emp , man , wor ).

You should consider moving some of that initialization code into your constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM