0

This else if statement works as intended, except after it is finished running it comes to this ConcurrentModificationException error. Im thinking it might be from the for each loop but I am not too sure.

 else if(input == 2){
            System.out.println("Enter a period");
            int periodInput = sc.nextInt();
            System.out.println("Enter the students name (Dont include a space between the first and last name)" );
            String nameInput = sc.next();
            if(periodInput == 3){
                for(Student a: period3){
                    if(a.getName().equals(nameInput)){
                        period3.remove(a);
                        System.out.println(period3);
                    }else{
                        System.out.println("No student was found with this name");
                    }
                }
            }

5 Answers5

0

You can't modify the structure of collection while iterating it. You should prefer using Iterator. Use something like this:

          for(Iterator<Student> stItr = period3.iterator(); stItr.hashNext();){
                Student a = stItr.next();
                if(a.getName().equals(nameInput)){
                    stItr.remove();
                    System.out.println(period3);
                }else{
                    System.out.println("No student was found with this name");
                }
            }
Deepak Kumar
  • 1,246
  • 14
  • 38
0

EDIT: I figure out how to use for loops instead of a for each loop to solve my problem

if(periodInput == 3) {
                for(int i=0; i<=period3.size()-1; i++) {
                    if(period3.get(i).getName().equals(nameInput)) {
                        period3.remove(i);
                        System.out.println(period3);
                        break;
                    }
                }
            }
            else if(periodInput == 5) {
                for(int i=0; i<=period5.size()-1; i++) {
                    if(period5.get(i).getName().equals(nameInput)) {
                        period5.remove(i);
                        System.out.println(period5);
                        break;
                    }
                }
            }
0

As mentioned in previous answers, Java does't allow concurrent modification to a list in a for-each loop. The solution you are going ahead with will work as you are not looping on the list itself but using an integer to loop and after each iteration you are checking for size of array(which is modified after each removal) thus the loop is aware of the removal. You can try something like this also:

            else if(input == 2){
            List<Student> removalList = new ArrayList<Student>();
            System.out.println("Enter a period");
            int periodInput = sc.nextInt();
            System.out.println("Enter the students name (Dont include a space between the first and last name)" );
            String nameInput = sc.next();
            if(periodInput == 3){
                for(Student a: period3){
                    if(a.getName().equals(nameInput)){
                        removalList.add(a);
                    }
                }
                if(!removalList.isEmpty()){
                    for(Student stu: removalList){
                        period3.remove(stu);
                    }
                    System.out.println(period3);
                } else{
                    System.out.println("No student was found with this name");
                }
            }
Akshay Mishra
  • 189
  • 1
  • 8
0

Define your "period03" lsit like as below,

List<Student> period3 = new CopyOnWriteArrayList<Student>();

if there have any issue inform me.

avishka eranga
  • 105
  • 1
  • 11
0

You can't modify collection you are iterating (iterator is being corrupted). Imagine if you'd iterate by index from i=0 to size() and update the collection on the way...

Use filter instead of for-loop (assuming period3 is a collection - list)

change this

for(Student a: period3){
    if(a.getName().equals(nameInput)){
        period3.remove(a);
        System.out.println(period3);
    }else{
        System.out.println("No student was found with this name");
    }
 }

to

period3 = period3.stream().filter(student -> !student.getName().equals(nameInput)).collect(toList());

If Java 8+ is not an option - create resulting collection and add eligible items to it rather than mutating the original list.

Azee
  • 1,809
  • 17
  • 23