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
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
}
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.