does someone help me with this error in java?

0

Basically I have a problem that says:

When it comes to autographing, the queue of people who want one of their own can be whole blocks. But it happens that the people who make the tail of Serena's autographs are very impatient.

If, when it comes to being formed, the number of people in the queue is equal to or greater than a maximum that each person defines, they simply do not form in it. Also, if at any time the size of the queue exceeds that maximum all those.

With that maximum prefer to leave, which is funny considering that with only one that does and the size would be below that value (apart from impatient not have very good logic to say).

For example, if a person arrives whose maximum is 5 and at that moment the queue has 8 people, it will not be formed.

On the other hand, if a person arrives whose maximum is 10 and at that moment the queue has 9 people, it will be formed, but at the moment it exceeds that value, both it and all others with that same maximum will come out.

This is my code

import java.util.*;

public class Main {

public static void main(String[] args) {
    LinkedList<Integer> Tamaño = new LinkedList<Integer>();
    LinkedList<Long> Cedula = new LinkedList<Long>();
    Iterator<Integer> Iterador = Tamaño.listIterator();
    Scanner entrada = new Scanner(System.in);
    String S;
    int i, T, L = 0;
    short R = 0;
    boolean vall = true;
    long C, N;
    while (vall) {
        S = entrada.next();
        if (S.equals("fin")) {
            break;
        }
        if (S.equals("ingresa")) {
            C = entrada.nextLong();
            Cedula.add(C);
            T = entrada.nextShort();
            Tamaño.add(T);
            if(Tamaño.get(Tamaño.lastIndexOf(T)) <= Tamaño.size()){
                Tamaño.removeLast();
            }
            R++;
            Iterador = Tamaño.listIterator();
            while(Iterador.hasNext()){
                i=Iterador.next();
                if((Tamaño.get(0))<= (Tamaño.size()) && R>1){
                    Tamaño.removeFirst();
                }
                else{
                    if(Tamaño.get(Tamaño.indexOf(i))<= Tamaño.size()&&  
                    R>1){
                        Tamaño.remove(Tamaño.indexOf(i));
                    }
                }   
            }
        }
            if (S.equals("cuenta")) {
                    N = Tamaño.size();
                    System.out.println("Personas en la cola: " + N);
            }
        }
    }
}

If they analyze well, everything works "well" until the entry is:

  

enter 1000002 4

     

enter 1000003 4

     

enter 1000004 4

     

enter 1000005 4

     

enter 1000006 5

and I get the following error:

  

Exception in thread "main" java.util.ConcurrentModificationException       at java.util.LinkedList $ ListItr.checkForComodification (LinkedList.java:966)       at java.util.LinkedList $ ListItr.next (LinkedList.java:888)       at Main.main (Main.java:32)   C: \ Users \ user \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml: 53: Java returned: 1

I do not know what to do: c.

    
asked by Ricardo Arias 19.03.2018 в 21:08
source

1 answer

3

Your problem is here:

        Iterador = Tamaño.listIterator();
        while(Iterador.hasNext()){
            i=Iterador.next();
            if((Tamaño.get(0))<= (Tamaño.size()) && R>1){
                Tamaño.removeFirst();
            }
            else{
                if(Tamaño.get(Tamaño.indexOf(i))<= Tamaño.size()&&  
                R>1){
                    Tamaño.remove(Tamaño.indexOf(i));
                }
            }   
        }

Basically what the error says is that you are going through a list and modifying it at the same time. That is, you are going through the Size list, and within the while loop you are deleting elements from that same list, which can not be done because it gives you a ConcurrentModificationException. The best thing you can do is create an auxiliary list, SizeAux for example, and within the loop that runs Size, add to SizeAux the elements that are worth you. Once out of the while, you copy the SizeAux values in Size.

    
answered by 20.03.2018 в 11:25