Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:13:04+00:00 2026-06-14T12:13:04+00:00

I’m writing a text file crawler program and keep getting a ConcurrentModificationException. I know

  • 0

I’m writing a text file crawler program and keep getting a ConcurrentModificationException. I know it has something to do with using Iterator but I’m not sure how to fix it. Please help!

Exception is:

Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(unknown Source)
        at java.util.LinkedHashMap$KeyIterator.next(Unknown Source)
        at TextCrawler.main(TextCrawler.java:112)

Code:

while(it1.hasNext() && wordCount2 < wordCountToFind2) { //while there are more files to be searched and the wordCount is less than max occurrences 
           wordCount2 = 0;
           Iterator it3 = occurrencesVector.iterator();
           while(it3.hasNext()) { //get current wordCount
             wordCount = (Integer)it3.next();
             wordCount2 += wordCount;
             System.out.println("WordCount2...." + wordCount2); //Test
           }
           String nextFile = (String)it1.next(); //this is line 112
           System.out.println("nextFile...." + nextFile + "\n" + "\n"); //Test
           if(i > 0) { //skips the initial input filename (which is at the start of filenameSet) so it's not checked twice
             System.out.println("Start searchFile method"); //Test
             try{txtCr2.searchFile(nextFile, wordToFind2, wordCountToFind, caseSensitive);} //call searchFile method
             catch(IOException e){System.out.println("txtCr2 exception, searchFile method didn't happen!"); e.printStackTrace();};
           }
           i++;
           System.out.println("i = " + i);
         }

Full code for searchFile method:

public void searchFile(String filename, String wordTF, String wordCountTF, String caseS) throws IOException
 {
    FileReader aFileReader = new FileReader(new File(filename)); //make the file readable
    BufferedReader aBufferedReader = new BufferedReader(aFileReader);
    String newFile, lineFromFile, updatedLine = "", filePattern = "\\([a-zA-Z0-9]{1,32}.txt\\)", outputMessage = "";
    char aChar;
    int wordCount = 0, occurrencesToFind;
    occurrencesToFind = Integer.parseInt(wordCountTF); //convert passed down String to int
    filenameSet.add(new String(filename)); //add the filename to the LindedHashSet

    while((lineFromFile = aBufferedReader.readLine()) != null) { //while lineFromFile is not empty
      for(int i = 0; i < lineFromFile.length(); i++) { 
        aChar = lineFromFile.charAt(i); //go through each line character by character
        if(Character.isLetterOrDigit(aChar) || Character.isWhitespace(aChar) || aChar == '(' || aChar == ')' || aChar == '.') 
          if(aChar == '(') //if the character is ( then add a space in front of it
            updatedLine = updatedLine +  " " + aChar;  
          else
          if(aChar == ')') //if the character is ) then add a space after it
            updatedLine = updatedLine + aChar + " ";
          else
            updatedLine = updatedLine + aChar; //if the character is a letter, digit or whitespace just add it to the updatedLine
      }    //all unwanted punctuation is removed
    } 
    aFileReader.close();
    aBufferedReader.close();

    String [] fileArray = updatedLine.toLowerCase().split(" "); //split the updatedLine into an array at the whitespaces.

    //for(int i = 0; i < fileArray.length; i++) //Test
      //System.out.println(fileArray[i]);

    boolean fileSearched = false;

    while(wordCount < occurrencesToFind && !fileSearched) { //while the wordCount is less than the required wordCount and the file hasn't been completely searched.
      if(caseS.equals("1")) { //if the word to be checked is case sensitive
        for(int j = 0; j < fileArray.length; j++) { //for the length of the array
          if(fileArray[j].equals(wordTF)) //if a word in the fileArray exactly equals the word to be searched
            wordCount++; //add a count of one to the wordCount
          if(fileArray[j].matches(filePattern)) { //if a word in the fileArray matches the filePattern e.g. (filename.txt)
            newFile = fileArray[j].substring(1,fileArray[j].length() -1); //newFile is equal to the filePattern with round brackets removed.
            System.out.println("New File found..." + newFile); //Test
            filenameSet.add(new String(newFile)); //add it to the LinkedHashSet
          }
        }
        fileSearched = true; //file has been searched
      }
      else { //else if the word to be checked isn't case sensitive
        for(int j = 0; j < fileArray.length; j++) { //for the length of the array
          if(fileArray[j].toLowerCase().equals(wordTF.toLowerCase())) //not case sensitive so make both search word and fileArray word lower case
            wordCount++; //if they are equal add a count of one to the word count
          if(fileArray[j].matches(filePattern)) { //if a word in the fileArray matches the filePattern e.g. (filename.txt)
            newFile = fileArray[j].substring(1,fileArray[j].length() -1); //newFile is equal to the filePattern with round brackets removed.
            System.out.println("New File found...." + newFile); //Test
            filenameSet.add(newFile); //add it to the LinkedHashSet
          }
        }
        fileSearched = true; //file has been searched
      }
    }
    occurrencesVector.addElement(new Integer(wordCount)); //add the wordCount to the occurrences LinkedHashSet
    System.out.println("occurrencesVector contains.." + occurrencesVector); //Test
    System.out.println("filenameSet contains.." + filenameSet);  //Test
    System.out.println("End of searchFile method.\n\n"); //Test
 }

occurrencesVector:

public class TextCrawler
{
 static LinkedHashSet<String> filenameSet = new LinkedHashSet<String>();
 static Vector<Integer> occurrencesVector = new Vector<Integer>();

it1:

System.out.println("\nStarting initial file search in..." + startFile2);
         try{txtCr.searchFile(startFile2, wordToFind2, wordCountToFind, caseSensitive);}
         catch(IOException e){System.out.println("txtCr exception");};

         boolean nextFileSearched = false;
         int wordCount, wordCount2 = 0, wordCount3, wordCount4 = 0; 
         Iterator it1 = filenameSet.iterator();
         Iterator it2 = occurrencesVector.iterator();
         int i = 0;

         while(it2.hasNext()) { //get current wordCount
           wordCount = (Integer)it2.next();
           wordCount2 += wordCount;
         }

         while(it1.hasNext() && wordCount2 < wordCountToFind2) { //while there are more files to be searched and the wordCount is less than max occurrences 
           wordCount2 = 0;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:13:05+00:00Added an answer on June 14, 2026 at 12:13 pm

    ConcurrentModificationException means that you are using an “expired” iterator.

    The iterator is “expired” since the collection it is currently crawling has changed.

    Here is a great code example to demonstrate:

    public static void main(String[] ar) {
        LinkedList<Integer> l = new LinkedList<Integer>();
    
        l.add(1);
        l.add(2);
        l.add(3);
    
        Iterator<Integer> i1 = l.iterator();
    
        System.out.println(i1.next());
        System.out.println(i1.next());
    
        // Here we modify the content of "l"
        l.add(4);
    
        // After modifying the content of "l" we try to get the next element of the iterator: this will throw an exception
        System.out.println(i1.next());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a reasonable size flat file database of text documents mostly saved in
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.