0

I have a code to read the contents from a file and write in another file as follows. The input file consists of call logs and the code reads from the file and creates maps for a particular call. A particular agent can take one call at a time and is on call until you reach the line containing Hang up. When the code reaches the line I should write the contents of the map for that particular call in text file and flush the map.

inputfile.txt

[DialerManager  ] 15:40:35.688 MakeCall,CallId51,9972454323
[RandomId63  ] 15:40:57.562 CallId51 CallTransfer,Jimmy,109
[CallId51 ] 15:40:59.633      RandomId63 CALLSTATUS,CONNECTED
[TempId78  ] 15:51:27.586 Jimmy: Hang up
[DialerManager  ] 15:40:35.688 MakeCall,CallId52,1234567890
[RandomId68  ] 15:40:57.562 CallId52 CallTransfer,James,100
[CallId52 ] 15:40:59.633      RandomId68 CALLSTATUS,CONNECTED
[TempId74  ] 15:51:27.586 James: Hang up
[DialerManager  ] 15:40:35.688 MakeCall,CallId53,3456780912
[DialerManager  ] 15:40:40.688 MakeCall,CallId54,9807652341
[RandomId01  ] 15:40:57.562 CallId53 CallTransfer,John,80
[CallId53 ] 15:40:59.633      RandomId01 CALLSTATUS,CONNECTED
[RandomId600  ] 15:40:57.562 CallId51 CallTransfer,Jimmy,109
[CallId54 ] 15:40:59.633      RandomId600 CALLSTATUS,CONNECTED
[TempId100  ] 15:53:27.586 John: Hang up
[TempId345  ] 15:56:27.586 Jimmy: Hang up

Here I want to create a map for each call and then flush the map when it reaches Hang up. Then the receiver will proceed to a new call thereby creating a new map for the receiver. I have the code below.

code.java

import java.io.*;
import java.util.HashMap;

public class ParseMapTest {
    public static void main(String[] args) throws IOException {
        BufferedReader reader;
        HashMap<String, HashMap<String, String>> hMap = new HashMap<>();
        try {
            reader = new BufferedReader(new FileReader("mapEx.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains("MakeCall")) {
                    String[] words = line.split(",|:| ");
                    for (int i = 0; i < words.length; i++) {
                        if (words[i].startsWith("CallId")) {
                            String header = words[i];
                            if (!hMap.containsKey(header)) {
                                hMap.put(header, new HashMap<>());
                            }
                        }
                    }
                } else if (line.contains("CallTransfer")) {
                    String header = line.split(" ")[4];
                    // System.out.println(header);                
                    if (hMap.get(header) == null) {
                        hMap.put(header, new HashMap<>());
                    }
                    hMap.get(header).put("Agent", line.split(",")[1]);
                    hMap.get(header).put("AgentId", line.split(",")[2]);
                } else if (line.contains("CALLSTATUS")) {
                    String headerBrack = line.split(" ")[0];
                    String header = headerBrack.replaceAll("[\\[]", "");

                    if (hMap.get(header) == null) {
                        hMap.put(header, new HashMap<>());
                    }
                    String[] lineSplit = line.split(",");
                    hMap.get(header).put("CALLSTATUS", lineSplit[lineSplit.length - 1]);
                } else if (line.contains("Hang Up")) {
                    String agent = line.split("\\.|:")[3].substring(4);
                    for(String s: hMap.keySet()){
                        HashMap<String, String> switchMap = hMap.get(s);
                        if(switchMap.containsValue(agent)) {
                            System.out.println(s + hMap.get(s));
                            String map = s + hMap.get(s);
                            FileWriter fw = new FileWriter("out.txt", true);
                            BufferedWriter bw = new BufferedWriter(fw);
                            PrintWriter out = new PrintWriter(bw);
                            out.println(map);
                            out.close();
                            hMap.remove(s);
                         }                        
                     }
                 }
            }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(hMap);
}
}

I am creating a file out.txt where I am writing the inner maps which have reached Hang up and deleting them from hMap but I am getting java.util.ConcurrentModificationException. How can I rectify the error? Thanks in advance!

ttzn
  • 2,543
  • 22
  • 26
Lax_Sam
  • 1,099
  • 2
  • 14
  • 32
  • what is the problem here ? cant you use a database ? – vels4j Apr 09 '19 at 05:12
  • The requirement is to output in a text file. I cannot use database. The output should be like this: The first hashmap with `Hang up` should be found out. So it is `{CallId51={Agent=Jimmy, AgentId=109,CallStatus=Connected}}`. Then when this reaches `Hang up` it should be written in a text file and flushed. Then the next map that reaches `Hang up` is for `James`. That should be written in the same file and flushed and so on. – Lax_Sam Apr 09 '19 at 05:44
  • See this : https://stackoverflow.com/questions/1884889/iterating-over-and-removing-from-a-map. Don't do `hMap.remove(s)` while iterating over `hMap.keySet()`. You can simply store the header containing the agent when you find it, then remove it outside the loop. – ttzn Apr 09 '19 at 07:35
  • 1
    See https://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re?r=SearchResults – Raedwald Apr 09 '19 at 07:36

1 Answers1

-1

This exception occurs when you try to remove Items from ArrayList or Hashmap in a loop. To avoid such exception make a separate ArrayList of items which you want to remove then in a separate loop remove the items.

Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36