-1

so i'm super stuck. I have a list of students with fname lname sNum etc. i'm trying to use my remove function and it doesn't work. i can't figure out where my problem is.

public void removeStudent(long sNumber) {
    //create student object
    Student student = new Student();
    //loop through the students 
    for (int i = 0; i < this.students.size(); i++) {
        //display students
        System.out.println(this.students.get(i));
        //condition if sNumber == sNumber 
        if(student.getsNumber() == sNumber){
            //remove the student from the list
            this.students.remove(student);
            System.out.println(this.students.get(i));
        }
        System.out.println("skipped the if statement");
    }
    /*for(Student student : this.students){
        if(student.getsNumber() == sNumber){
            this.students.remove(student);
        }
    }*/
}

this is where i call the method

case 3:
            // delete a student
            System.out.print("What is the Students sNumber: s");
            long sNum = input.nextLong();
            Student chkSNum = new Student();
            registry.getStudentBySNumber(sNum);
            chkSNum.setsNumber(sNum);
            if (registry.getStudentBySNumber(sNum) == chkSNum) {
                if (chkSNum.getsNumber() == sNum) {
                    registry.removeStudent(sNum);
                    System.out.println(chkSNum);
                }
            } else {
                System.out.println("Sorry no matches");
                // System.out.println(sNum);
                System.out.println(registry.getStudentBySNumber(sNum));
                System.out.println(chkSNum);
            }
            break;

my add student method works just fine.

    public void addStudent(Student student) {
    this.lastSNumber++;
    student.setsNumber(this.lastSNumber);
    this.students.add(student);
}
tyler
  • 11
  • 2

5 Answers5

3
if(student.getsNumber() == sNumber)

is comparing your number to a newly constructed, blank Student object. If you want to compare it to the ones you're iterating over, you'll need something like this:

if(this.students.get(i).getsNumber() == sNumber) {
    this.students.remove(i);
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • @Optional, it won't throw a CoMod exception because there's no proper iteration. It will skip an index if the loop continues, but there should probably be a `break;` in there anyhow. – shmosel Jan 29 '16 at 07:06
0

You are actually removing a new studend() object. Loop the list like this:

Student to_remove = null:
for(student s: this.students){
    if(s.getsNumber() == sNumber){
    to_remove = s;
    } 
    if(s!=null) break;
}
if(s!=null)this.students. remove(s);

This will not cause an Modification-Exception

osanger
  • 2,276
  • 3
  • 28
  • 35
  • this should replace my for loop? i'm so confused on why i'm having this problem. . – tyler Jan 29 '16 at 08:41
  • Yes you get this exceptin because you change the list while running through it. – osanger Jan 29 '16 at 08:58
  • _This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible._ [source](https://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html) – osanger Jan 29 '16 at 09:01
0

You should use the iterator to remove in the list. Something like

 Iterator<Student> it = this.students.iterator();
 while (it.hasNext()) {
 Student currentStudent = it.next();
 if(currentStudent.getsNumber() == sNumber)
  {
    it.remove();
   }
 }
Optional
  • 4,387
  • 4
  • 27
  • 45
-2

so i figured out what was wrong. my logic was dorky.

public void removeStudent(long sNumber) {
    //create student object
    //Student student = new Student();
    Student student = this.getStudentBySNumber(sNumber);
        //condition if sNumber == sNumber 
        if(student == this.getStudentBySNumber(sNumber)){
            System.out.println("removing the student from the list");
            this.students.remove(student);
            displayStudents();
        }
}

 case 3:
            // delete a student
            System.out.print("What is the Students sNumber: s");
            long sNumInput = input.nextLong();
            Student chkSNum2 = new Student();
            registry.getStudentBySNumber(sNumInput);
            chkSNum2.setsNumber(sNumInput);
            if (registry.getStudentBySNumber(sNumInput) != chkSNum2) {
                if (chkSNum2.getsNumber() == sNumInput) {
                    System.out.println("Student deleted...");
                    registry.removeStudent(sNumInput);
                }
            } else {
                System.out.println("Sorry no matches");
                System.out.println(registry.getStudentBySNumber(sNumInput));
                System.out.println(chkSNum2);
            }
            break;
tyler
  • 11
  • 2
-2

It's obviously something wrong in removeStudent. First you are creating a new Student and then removing using that instance.

What you should do is this:

public void removeStudent(long sNumber) {
  for (Student s : this.students) {
    if (s.getNumber() == sNumber) { 
      this.students.remove(s);
    }
  }
}

You can ofcourse also use the for loop you have in your code. It's the same thing. Just remember to remove the right instance.

Hope that helps.

Sten Roger Sandvik
  • 2,526
  • 2
  • 19
  • 16