0

I want to remove an object from an ArrayList based on the students name and each object has 5 elements. THE QUESTION: how do I remove the object from the text file.

ArrayList<Student> studentList= new ArrayList<Student>();
Iterator iter = studentList.iterator();

public DVD remove(String removeName) {
    while (iter.hasNext()) {
        for (Student stu : studentList) {
            if (stu .GetName().toUpperCase().contains(removeName.toUpperCase())) {
                System.out.println("Found And Removed");
                iter.remove();
                return stu ;
            }

            System.out.println("Not Found");
        }
    }
    return null;

}

Each student is read in from a text file. With one element per line.

Ex:

  John Smith
  Undergrad
  21 years old
  2010
  2014
  Pocahantas
  Professor
  369 years old
  1599
  1603
  Etc... // new person
  etc....
trama
  • 1,271
  • 3
  • 14
  • 24
  • 2
    What is the question? – Reimeus Apr 06 '13 at 20:31
  • @Reimeus how do I remove the object from the text file. – trama Apr 06 '13 at 20:32
  • Is this homework? If so you should flag it as such – Mike Apr 06 '13 at 20:34
  • Create a temporary file, write out the updated `ArrayList`, then replace the original file with the temporary one. – Reimeus Apr 06 '13 at 20:34
  • Maybe this question will help you on your way: http://stackoverflow.com/questions/1377279/java-find-a-line-in-a-file-and-remove If the line matches this name you can remove it and using a counter you will be able to remove the next 4 lines too. – Menno Apr 06 '13 at 20:34

3 Answers3

1

I don't think you need an Iterator:

    public Student remove(String removeName) {
        for (Student stu : studentList) {
            if (stu.getName().toUpperCase().contains(removeName.toUpperCase())) {
                System.out.println("Found and Removed");
                studentList.remove(stu);
                return stu;
            }
         }
         System.out.println("Not Found");
         return null;
    }
Noe
  • 378
  • 1
  • 7
  • 1
    Actually that's very likely to cause a `java.util.ConcurrentModificationException` if studentList is an `ArrayList`. – Random42 Apr 06 '13 at 20:42
  • 1
    In this case, the exception is not thrown as the loop is ended after the elimination, it's not evaluated for next element again. However, you're right. If the purpose of this function were to remove _several_ matching elements in the `ArrayList`, a `ConcurrentModificationException` will be thrown exactly when the loop repeats after removing an element. – Noe Apr 06 '13 at 21:15
0

You are using the iterator in the wrong way; if you are iterating with an external iterator there is no need for the for statement:

while (iter.hasNext()) {
    Student stu = iter.next();
    if (stu .GetName().toUpperCase().contains(removeName.toUpperCase())) {
        System.out.println("Found And Removed");
        iter.remove();
        return stu ;
    }
    System.out.println("Not Found");
    }
}

If you know the student before (or it's position) you can simply invoke remove method from the List interface but if you want to delete it while you are iterating through the collection, then an external iterator is the way with most of Java Collections.

Random42
  • 8,989
  • 6
  • 55
  • 86
0

If I understand correctly, you want to delete the 5 lines corresponding to the name of the student which has to be removed from the text file.

If this is what you want to do then refer to

Find a line in a file and remove it

This has the snippet for removing one single line. What you need to do is to delete the next 5 lines as soon as you encounter the name to be removed.

Community
  • 1
  • 1
prashant
  • 1,805
  • 12
  • 19