Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.Writer;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.nio.file.StandardOpenOption;
- import java.util.Formatter;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.TreeMap;
- public class Total {
- public static void main(String[] args) {
- Scanner in = null;
- try {
- in = new Scanner(Paths.get("shukei.csv"));
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- in.useDelimiter("\\s*,\\s*|\n");
- Map<String, Double> total = new TreeMap<String, Double>();
- while (in.hasNextLine()) {
- if (!in.hasNext()) break;
- String key = in.next();
- in.next(); // ignore date
- in.next(); // ignore item
- double amount = in.nextDouble();
- if (!total.containsKey(key)) total.put(key, 0.);
- total.put(key, total.get(key) + amount);
- }
- in.close();
- Writer out = null;
- try {
- out =
- Files.newBufferedWriter(Paths.get("result.csv"),
- Charset.forName("UTF-8"), StandardOpenOption.WRITE,
- StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- Formatter f = new Formatter(out);
- for (Entry<String, Double> e: total.entrySet()) {
- f.format("%s,%.1f\n", e.getKey(), e.getValue());
- }
- f.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement