How can I remove an object from an ArrayList? If you run my code, my delete method in the Team class does not seem to be able to remove the object that I typed from the keyboard.
the remove() method seems so have no effect even though that is how you remove an object from arraylist object that I removed is still there
delete method I created to remove object from ArrayList:
HTML Code:
public void delete() throws IOException {
System.out.println("Enter the name of the file that you wish to delete information from: ");
String name = scan.next();
File w = new File(name);
if(w.exists())
{
ArrayList<Player> x = new ArrayList<Player>();
try {
Stream<String> lines = Files.lines(Paths.get(name));
lines.forEach((String t) -> {
String[] parse = t.split(" ");
if(parse.length<5) return;
x.add(new Player(parse[0], parse[1], parse[2], parse[3], parse[4]));
});
} catch (IOException ex) {
System.out.println("Unable to open student file." + ex.toString());
}
System.out.println("Size of Player ArrayList: " + x.size());
System.out.println();
for(int i=0;i<x.size();i++){ //Prints objects from chosen file to console
System.out.println(x.get(i));
}
System.out.println("How many people would you like to remove? ");
int num_people = scan.nextInt();
System.out.println("Enter first, last, goals, caps, and assists:");
for(int i=0; i<num_people; i++){
String fstname2 = scan.next();
String lstname2 = scan.next();
String goals = scan.next();
String caps = scan.next();
String assists = scan.next();
x.remove(new Player(fstname2,lstname2,goals,caps,assists));
}
System.out.println("Done.");
System.out.println();
for(int i=0;i<x.size();i++){
System.out.println(x.get(i));
}
BufferedWriter q = new BufferedWriter(new FileWriter(w));
for(Player p : x){
q.write(p + "\n");
q.newLine();
}
q.close();
}
else{
System.out.println("File does not exist.");
}
}