0

I cannot compile this program which from a file with this format (where "Via" is the italian for "road")

Via Emilia-->Via Botti
Via Castello-->Via Emilia
Via Appia-->Via Botti
Via Botti-->Via Emilia
Via Castello-->Via Amendola
Via Appia-->Via Ancona
Via Castello-->Via Botti
Via Matteotti-->Via Carso
Via Ancona-->Via Matteotti
Via Carso-->Via Matteotti

And at the third questions asks me to find which roads are recursive one into another (like a->b and b->a), but when I try to put conditions to remove the possibility of having duplicate answers (like a and b are recursive and b and a are recursive) I get this error "java.util.ConcurrentModificationException", what does it mean?

This is the main

public static void main(String[] args) throws Exception{

    ArrayList<percorso> perc = new ArrayList<percorso>();

    String line;
    BufferedReader br = new BufferedReader(new FileReader("vie.txt"));

    while ((line = br.readLine())!=null){
    percorso p = new percorso (null,null);
    String [] e = line.split("-->");
    p.from = e[0];
    p.to = e[1];
    perc.add(p);
    }
    br.close();
    //punto1(perc);
    //punto2(perc);
    punto3(perc);
}       

This is the method to compile the answer at the third question

    public static void punto3(ArrayList<percorso> perc) throws Exception{

    BufferedWriter bw=new BufferedWriter(new FileWriter("3.html"));
    bw.write("<html> \n <body> \n");
    bw.write("<h1> Punto 3 </h1> \n");
    bw.write("<h3><i> Trovare le coppie di strade che formano un anello chiuso </h3></i> \n");
    ArrayList<percorso> perc2 = new ArrayList<percorso>();
    for (percorso k: perc)
        perc2.add(k);

    ArrayList<String[]> Arr = new ArrayList<String[]>();

    for (percorso i: perc2){
        for (percorso j: perc2){
            if (i.from.equals(j.to) && i.to.equals(j.from)){
                String [] e = {i.from,i.to};
                Arr.add(e);
            }
        }
    }

    ArrayList<String[]> Arr2 = new ArrayList<String[]>();
    for (String [] z: Arr){
        Arr2.add(z);}

    for (String [] f: Arr){
        for (String [] j: Arr2){
            if (f[0].equals(j[1]) && j[0].equals(f[1]))
                Arr.remove(j);
        }
    }
    for (String [] x: Arr)
    bw.write("le vie "+x[0]+", "+x[1]+" formano un anello chiuso <br>");
    bw.write("</body> \n </html> \n");
    bw.close();
    }

And the class percorso is the following

   public class percorso {
   String from;
   String to;

   public percorso (String from,String to){
    this.from = from;
    this.to = to;
   }
   }
Leonardo
  • 3
  • 2
  • Can you add the stacktrace or say in which line you get that exception? – Pablo Matias Gomez Jun 28 '17 at 14:23
  • 4
    `Arr.remove(j);` <- You're changing the content of `Arr` while you're iterating over it. You can do that, but you need to use an `Iterator` reference to do it rather than a `for` or enhanced `for` loop. – JonK Jun 28 '17 at 14:24
  • 1
    Unrelated: don't get me wrong, but this code is pretty much unreadable. Indenting matters. Names matter. You make your life (and those of any person reading your code) 10 times harder than it should be. Follow java naming conventions (class names go CamelCase). Don't use singlee char names, don't use var names that start with UpperCase. Sorry, terrible. – GhostCat Jun 28 '17 at 14:39
  • And hint: you simply put up the exception name into your favorite search engine; that is how you try to fix it ;-) – GhostCat Jun 28 '17 at 14:40

2 Answers2

0

You are trying to remove a record from arraylist while you are looping over it: Arr.remove(j). This causes the ConcurrentModificationException.

Instead of removing it from the arraylist while looping, you can add it in a separate list (eg valuesToRemove). This keeps all records you want to remove and then after your loop, call Arr.removeAll(valuesToRemove);

stevendv
  • 64
  • 7
0

So the problem is that you are using Arr.remove(j); inside a for loop, and you can't do that.

You can read this: How to avoid "ConcurrentModificationException" while removing elements from `ArrayList` while iterating it?

And this Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop

But basically, what you have to do is using an iterator to remove elements, like this, for example:

Iterator<String> iter = myArrayList.iterator();

while (iter.hasNext()) {
String str = iter.next();

if (someCondition)
    iter.remove();
Pablo Matias Gomez
  • 6,614
  • 7
  • 38
  • 72