1

I want to read a CSV file , like this :

DATE=2014-03-08;ID=01;AVG=10

public void readInputStream(InputStream in) throws IOException {
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ";";

    try {


        br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        while ((line = br.readLine()) != null) {
            String[] date= line.split(cvsSplitBy);
            if (line.contains("DATE")){
            System.out.println(date[0]);
        }}
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

i want to sum the avg by date and id ? any help

4
  • You need to provide more information. What is the problem, what is your question? Commented Mar 31, 2014 at 8:42
  • i want to get the date value ' 2014-03-08' Commented Mar 31, 2014 at 8:44
  • And what issue do you get? Does the provide code works? If not what is the result? You really need to provide more information. Commented Mar 31, 2014 at 8:46
  • This is a CSV, therefore you can use OpenCSV ;) Commented Mar 31, 2014 at 8:49

2 Answers 2

2
import java.io.*;
import java.util.*;

public class CsvSum{

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

public static void main(String args[]) throws Exception{
    File file = new File("test.csv");
    Scanner scanner = new Scanner(file);
    while(scanner.hasNext()){
        String line = scanner.next();
        String[] columns = line.split(";");

        String date = columns[0].replace("DATE=","");
        String id = columns[1].replace("ID=","");
        int avg = Integer.parseInt(columns[2].replace("AVG=",""));

        String key = date + "_" +id;
        if(!map.containsKey(key)){
            map.put(key,avg);
        }else{
            Integer existing = map.get(key);
            map.put(key, existing + avg);
        }
    }

    System.out.println(map);
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your solution is simpler then mine, like it.
0
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Some {
    public static final String FIELDS_DELIMITER = ";";
    public static final String KEY_VALUE_DELIMITER = "=";

    public static void main(String[] args) throws IOException {
        readInputStream(new FileInputStream("test.csv"));
    }

    public static void readInputStream(InputStream in) throws IOException {
        BufferedReader br;
        String line;
        Map<GroupKey, Integer> groupedValues = new HashMap<GroupKey, Integer>();

        try {
            br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            while ((line = br.readLine()) != null) {
                String[] fields = line.split(FIELDS_DELIMITER);

                HashMap<String, String> fieldsValues = new HashMap<String, String>();
                for (String field : fields) {
                    String[] keyAndValue = field.split(KEY_VALUE_DELIMITER);
                    fieldsValues.put(keyAndValue[0], keyAndValue[1]);
                }
                GroupKey gk = new GroupKey();
                gk.id = fieldsValues.get("ID");
                gk.date = fieldsValues.get("DATE");

                if (!groupedValues.containsKey(gk)) {
                    groupedValues.put(gk, 0);
                }
                Integer fieldAvg = Integer.valueOf(fieldsValues.get("AVG"));
                groupedValues.put(gk, groupedValues.get(gk) + fieldAvg);
            }
            br.close();

            for (Map.Entry<GroupKey, Integer> groupKeyAndSum : groupedValues.entrySet()) {
                GroupKey gk = groupKeyAndSum.getKey();
                Integer sum = groupKeyAndSum.getValue();
                System.out.println("ID " + gk.id + ", date " + gk.date + ": " + sum);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static class GroupKey {
        private String date;
        private String id;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            GroupKey groupKey = (GroupKey) o;

            return !(date != null ? !date.equals(groupKey.date) : groupKey.date != null) &&
                    !(id != null ? !id.equals(groupKey.id) : groupKey.id != null);

        }

        @Override
        public int hashCode() {
            int result = date != null ? date.hashCode() : 0;
            result = 31 * result + (id != null ? id.hashCode() : 0);
            return result;
        }
    }
}

2 Comments

i'm getting java.lang.NumberFormatException: null , at Integer fieldAvg = Integer.valueOf(fieldsValues.get("AVG"));
DATE=2014-03-08;ID=01;VAL=81;AVG=10; DATE=2014-03-08;ID=01;VAL=12;AVG=20; DATE=2014-03-08;ID=01;VAL=25;AVG=30; DATE=2014-03-08;ID=01;VAL=87;AVG=40;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.