0

I am just trying to add some objects to an ArrayList in eclipse, but i keept getting an error (Syntax error, insert "... VariableDeclaratorId" to complete FormalParameterList) under the 'persons.add(one);'. Any idea what I am doing wrong?

package thequestion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class PersonComparator implements Comparator<Person>{

   @Override
   public int compare(Person o1, Person o2) {
       return 0;
   }

   Person one = new Person("Kevin", "Gresmer");
   List<Person> persons = new ArrayList<Person>();

   persons.add(one);

   public void sortByLastName(List people) {
       Comparator comp = new PersonComparator();
       Collections.sort(people, comp);
   }
}


public class Person {
    private String firstName = null;
    private String lastName = null;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}
3
  • 1
    Your persons.add(one); statement isn't within a method. Only declarations can be directly within a class. Commented Mar 26, 2015 at 16:19
  • When do you think persons.add(one); should be invoked? Commented Mar 26, 2015 at 16:20
  • (And what is the point of persons at all here?) Commented Mar 26, 2015 at 16:23

4 Answers 4

1

The line persons.add(one) is a statement, and you can't have a statement outside a block of code (method, constructor, etc.). Also, your Comparator should contain code related to the comparison you are doing. I don't think it's the right place to store a list or implement the sortByLastName() method.

Sign up to request clarification or add additional context in comments.

Comments

0

You can only initialize the instance variables in class and outside the methods. You initialized persons. Then to apply methods, you need to put that line into some methods. So, may be you can update your Sort Method or create new one and make reference to that method from your sort method.

  • The first option as explained in the above comment.

    class PersonComparator implements Comparator{

       @Override
       public int compare(Person o1, Person o2) {
           return 0;
       }
    
       Person one = new Person("Kevin", "Gresmer");
       List<Person> persons = new ArrayList<Person>();
    
       private void addPersons(){
         persons.add(one);
       }
                      //persons.add(one);
    
       public void sortByLastName(List people) {
           addPersons();
           Comparator comp = new PersonComparator();
           Collections.sort(people, comp);
       }
    }
    

The point is we cannot write anything except the Instance variables. We have to cover them with methods. I hope this will solve your issue.

Comments

0

put below under sortByLastName method. Currently below code snippet not lying under any method.

    persons.add(one);

Correct code should be

public void sortByLastName(List people) {

       persons.add(one);
       Comparator comp = new PersonComparator();
       Collections.sort(persons , comp);
   }

please note :- But my answer is in terms of compilation error. Ideally comparator should have code only related to comparison. Nothing else.

3 Comments

Don't think it should go there =). He is lacking a constructor - give him one
I don't think that this code should be part of PersonComparator class at all.
@Pshemo you are correct. But my answer is in terms of compilation error
0

Obviously, you cannot do persons.add(...) in the middle of the class declaration.

You should certainly add one and persons in a main in the class Person, and keep your Comparator class clean with only the logic of comparison. The main will allow you to test your code. The same idea for the sortByLastName(...) you can declare it as static in Person class. Then you can test with the main, e.g.

// In class `Person`
public static void main(String[] args) {
   List<Person> persons = new ArrayList<Person>();
   Person one = new Person("Kevin", "Gresmer");
   persons.add(one);
   Person two = new Person("Elvis", "Presley");
   persons.add(two);
   System.out.println(sortByLastName(persons));
}

Note for the above to provide a sensible output, you need to add a toString() method to the Person class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.