I've been trying to remove an element in ArrayList using the remove() object but it is still not deleting anything. I already checked the list stored in my array and everything is displaying fine, but when I try to remove an element, It is not working. My goal was the user would input a name to remove and it would search through the list that is equal to that name and remove it. The List Array is set as global.
Here is the code:
public static void removeStudents() {
String removeName;
System.out.println("******REMOVE STUDENTS******");
System.out.print("Enter name you wish to remove: ");
removeName = hold.nextLine();
hold.nextLine();
for (int x = 0; x < fullName.size(); x++) {
if (fullName.get(x).equalsIgnoreCase(removeName)) {
fullName.remove(x);
}
}
}
ConcurrentModificationExceptionsoon, because you are modifying the content of the collection you are iterating over. You should use an iterator to remove List elements instead.readLine()? Is it possible that you are using aScannerand the firstreadLine()is returning an empty string: stackoverflow.com/q/13102045/85421