0
    public StudentLottery() {
     ArrayList<Student> list = new ArrayList<Student>();
}



    public void addStudents() {

    Scanner keyboard=new Scanner(System.in);
    String input;
    String id;
    String name;
    Student s;
    System.out.println("Enter? (y or n):");
    input=keyboard.nextLine();
    while (!(input.equals("n"))){
        System.out.println("Name:");
        name=keyboard.nextLine();   
        System.out.println("ID:");
        id=keyboard.nextLine();
        s=new Student(name,id);
        if (!(list.contains(s)))
            list.add(s);//error
        System.out.println("Enter? (y or n):");
        input=keyboard.nextLine();
    }

}

error occurs on list.add(s), I thought arrayLists could accept any type of object, but this arraylist only likes strings, so I'm unsure what I should be doing to fix this, so that my arraylist will accept student objects

jcreator says no suitable add method found

0

1 Answer 1

2

The list you've declared in the StudentLottery constructor is a local variable, not a field. You can't access it beyond the scope of the constructor. Perhaps you meant:

private ArrayList<Student> list;  // i.e. list is a field

public StudentLottery() {
    this.list = new ArrayList<Student>();  // initialize list in constructor
}
Sign up to request clarification or add additional context in comments.

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.