-1

I am a new bee in Java and want to map CSV columns according to their values. My orignal csv data is like:

 name,salary,address
 AAA,1000,kkk
 BBB,880,lll
 AAA,90,kkk
 CCC,700,mmm
 BBB,700,lll

Expected output should be in Hashmap Key Value pair of two columns name and salary like:

Key: AAA Value 1090
Key: BBB Value 1580
Key: CCC Value 700

This is my code:

        String line = "";
 InputStreamReader inStream = new 
       InputStreamReader(uc.getInputStream());
        buff = new BufferedReader(inStream);  
         try {
            while ((line = b.readLine()) != null) {
                String[] fields = parseCsvLine(line);

                // I dont know what to do here



            }


            b.close();
            Log.d("Get data from API", "Processing Stop");
        } catch (IOException e) {
            e.printStackTrace();
        }

 public  String[] parseCsvLine(String line) {
    // Create a pattern to match breaks
    Pattern p =
            Pattern.compile(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))");
    // Split input with the pattern
    String[] fields = p.split(line);
    for (int i = 0; i < fields.length; i++) {
        // Get rid of residual double quotes
        fields[i] = fields[i].replace("\"", "");
    }
    return fields;
}

1 Answers1

0

What you want is to split your line data into part. Then accumulate it to a Map

Map<String, Integer> result = new HashMap<>();

while ((line = b.readLine()) != null) {
   String[] parts = line.split(","); // Split your line, take `,` as delimeter
   String key = parts[0]; // This is your 'AAA'
   Integer value = Integer.parseInt(parts[1]); // This is your 1000
   if (result.get(key) == null) {
       result.put(key, value); 
   } else {
       result.put(key, result.get(key) + value); // There's already a value here, add it with new value
   }


}

The ugly part:

   if (result.get(key) == null) {
       result.put(key, value); 
   } else {
       result.put(key, result.get(key) + value); // There's already a value here, add it with new value
   }

Can replaced with new Java 8 Map utility:

 result.putIfAbsent(key, value); // Put (key, value) if key is absent from the map
 result.compute(key, value, (k, v) -> v + value);
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
  • Thanks, I am trying and let you know its working. – user2778724 May 18 '18 at 14:31
  • [@user2778724] You're welcome~. – Mạnh Quyết Nguyễn May 18 '18 at 14:33
  • The `String[] parts = line.split(",");` won't work for all cases, because the values can also contain commas. Such values are usually quoted, so it's better to use a csv parser here. Try commons-csv or opencsv. – Vladimir Vagaytsev May 18 '18 at 14:33
  • It's match with OP's format. Seriously, if the delimiter is not known beforehand, how did you split it??????? As openCSv use some common delimiter like tab, space, ... only. Add an entire library to just to read a simple file? lol. You made my day. – Mạnh Quyết Nguyễn May 18 '18 at 14:36
  • @Mạnh Quyết Nguyễn I have tried your code but displaying result give me following output {AAA=1000 90, BBB=880 700, CCC=700} – user2778724 May 18 '18 at 14:43
  • Did you try my code? Post your current code somewhere dude. – Mạnh Quyết Nguyễn May 18 '18 at 14:44
  • Map result = new HashMap<>(); try { while ((line = b.readLine()) != null) { String[] parts = parseCsvLine(line); // Split your line, take `,` as delimeter String key = parts[21]; // This is your 'AAA' String value = parts[16]; // This is your 1000 if (result.get(key) == null) { result.put(key, value); } – user2778724 May 18 '18 at 14:48
  • else { result.put(key, result.get(key) + value); // There's already a value here, add it with new value } } } catch (IOException e) { e.printStackTrace(); } System.out.println(result); – user2778724 May 18 '18 at 14:49
  • Sorry about the formatting :( – user2778724 May 18 '18 at 14:49
  • Can you post your code here https://pastebin.com/ It's easier for me to read – Mạnh Quyết Nguyễn May 18 '18 at 14:50
  • my csv has some special characters if I split it by , it is not splitting well thats why I am using parseCsvLine function – user2778724 May 18 '18 at 14:50
  • Give me a line sample of your csv. Did you parseCsv works well? Did you read the correct part of csv line? – Mạnh Quyết Nguyễn May 18 '18 at 14:52
  • "user_id","cookie_user","Item","title","description","resource_url","link","tags","Year","Month","Day","Hour","Minutes","Seconds","Day of Week","date","topic_coverage","view_coverage","complexity","community_id","type","scope","scope_topic_coverage" "cd330929-4725-441b-8091-5e4581722bd5","d2405086-fc67-bd45-345e-fb7fa7131dd5","30075056-e7a2-4636-8c68-3f277a3d3951","Calculating Probabilities of Combined Event? At grade","This lesson covers how to calculate the probability of combined – user2778724 May 18 '18 at 14:58
  • events.

    It is an educational content of CK-12 Foundation (to access some of the CK-12 contents you must be logged in).

    ","https://didactalia.net/comunidad/materialeducativo/recurso/calculating-probabilities-of-combined-event-at/30075056-e7a2-4636-8c68-3f277a3d3951","http://www.ck12.org/probability/Probability-of-Compound-Events/lesson/Calculating-Probabilities-of-Combined-Events/",
    – user2778724 May 18 '18 at 14:59
  • "calculating probabilities of combined events;complement;event;independent events;probability;trigonometry;binomial expansion;combinations;dependent;factorial;fundamental counting principle;independent;intersection;ma;",2017,11,10,8,34,1,"Monday","2017-12-10T08:34:01Z",0.060842433697347896,0.03796203796203795,0,"f22e757b-8116-4496-bec4-ae93a4792c28","resourceVisited","probability",0.16770186335403728 "cd330929-4725-441b-8091-5e4581722bd5","d2405086-fc67-bd45- – user2778724 May 18 '18 at 14:59
  • 345e-fb7fa7131dd5","2afe09d9-c069-4c86-964a-61c9c6a4209c","Recurso online para aprender que es la ciencia y sus ramas derivadas","

    material educativo sobre ciencia y ramas derivadas

    \n","https://didactalia.net/comunidad/materialeducativo/recurso/recurso-online-para-aprender-que-es-la-ciencia-y/2afe09d9-c069-4c86-964a-61c9c6a4209c","http://cienciaweb.com/material-teorico/ciencia/",
    – user2778724 May 18 '18 at 14:59
  • "fisica;quimica;naturaleza;ciencias de la naturaleza;ciencias naturales;ciencias físicas;ciencia;método científico;ciencias formales;hipótesis (método científico);teoría científica;",2017,11,10,8,33,37,"Monday","2017-12-10T08:33:37Z",0.060842433697347896,0.007042253521126762,22.044,"f22e757b-8116-4496-bec4-ae93a4792c28","resourceVisited","probability",0.060842433697347896 – user2778724 May 18 '18 at 15:00
  • Dude, post your csv header and some lines at pastebin.com. I can't read – Mạnh Quyết Nguyễn May 18 '18 at 15:00
  • You can access them on https://pastebin.com/KztHjmv3 – user2778724 May 18 '18 at 15:05
  • which field name you want to combine? Your parser function is wrong – Mạnh Quyết Nguyễn May 18 '18 at 15:21
  • I want to combine "complexity" and "scope" – user2778724 May 18 '18 at 15:28