/* * Copyright 2013 Wageningen Environmental Research * * Licensed under the EUPL, Version 1.1 or – as soon they * will be approved by the European Commission - subsequent * versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the * Licence. * You may obtain a copy of the Licence at: * * http://ec.europa.eu/idabc/eupl5 * * Unless required by applicable law or agreed to in * writing, software distributed under the Licence is * distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. * See the Licence for the specific language governing * permissions and limitations under the Licence. */ package javacsvparser; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author Peter Verweij */ public class JavaCsvParser { private static final int DATE_FIELD_INDEX = 0; private static final int TIME_FIELD_INDEX = 1; private static final int DIVESITE_FIELD_INDEX = 2; private static final int MAXDEPTH_FIELD_INDEX = 3; private static final int SIGHTINGS_FIELD_INDEX = 4; private static final String[] speciesOfInterest = new String[] { "HAWKSBILL", "GREEN", "BLACKTIP", "RIB", "HAMMERHEAD", "NURSE", // RIB="Caribbean reefshark" "SOUTHERN", "ROUGHTAIL", "EAGLE"}; private static final String[] smallGroupers = new String[] { "HIND", "CONEY", "GRAYSBY"}; private static final String[] rareGroupers = new String[] { "MARBLED"}; private static final String[] largeGroupers = new String[] { "YELLOWFIN", "YELLOWMOUTH", "NASSAU", "BLACK", "RED", "TIGER"}; // TIGER="Tiger grouper" private static final String[] unidentifiedGroupOfInterest = new String[] { "TURTLE", "SHARK", "STINGRAY", "GROUPER"}; public static void main(String[] args) { args = new String[] {"D:\\UserData\\DCBD\\maart2019\\seaSaba\\testReport.csv"}; System.out.println("ASSUMING: [Date];[Time];[Divesite];[Maxdepth];[Sightings]"); JavaCsvParser obj = new JavaCsvParser(); if ((args != null) && (args.length == 1)) { String csvFile = args[0]; File file = new File(csvFile); if (file.exists()) obj.run(csvFile); else System.out.println("The file does not exist"); } else System.out.println("Please provide the filename of a CSV file"); } public void run(String csvFile) { BufferedReader br = null; String line; String SPLIT_FIELD_SYMBOL = ";"; String SPLIT_SIGHTING_SYMBOL = ","; try { String reportHeader = "DATE;SHORTDATE;TIME;DIVESITE;MAXDEPTH"; for (String s:speciesOfInterest) reportHeader += ";"+s; for (String s:rareGroupers) reportHeader += ";"+s; for (String s:smallGroupers) reportHeader += ";"+s; for (String s:largeGroupers) reportHeader += ";"+s; for (String s:unidentifiedGroupOfInterest) reportHeader += ";"+s; System.out.println(reportHeader); Pattern pattern = Pattern.compile("-?\\d+"); br = new BufferedReader(new FileReader(csvFile)); br.readLine(); // headers while ((line = br.readLine()) != null) { Map soi = createSpeciesOfInterest(speciesOfInterest); Map rgoi = createSpeciesOfInterest(rareGroupers); Map sgoi = createSpeciesOfInterest(smallGroupers); Map lgoi = createSpeciesOfInterest(largeGroupers); Map uoi = createSpeciesOfInterest(unidentifiedGroupOfInterest); String[] dive = line.split(SPLIT_FIELD_SYMBOL); String date = dive[DATE_FIELD_INDEX]; String shortDate = date.substring(3, 6) + "20" + date.substring(6); String time = dive[TIME_FIELD_INDEX]; String diveSite = dive[DIVESITE_FIELD_INDEX]; String maxDepth = dive[MAXDEPTH_FIELD_INDEX]; if (dive.length>4) { String[] sightings = dive[SIGHTINGS_FIELD_INDEX].split(SPLIT_SIGHTING_SYMBOL); for (int i=0; i createSpeciesOfInterest(String[] list) { Map result = new HashMap<>(); int defaultCount = 0; for (String soi: list) result.put(soi, defaultCount); return result; } private boolean isSpeciesOfInterest(String species, int count, Map counter) { boolean isSpeciesOfInterest = false; for (String s: counter.keySet()) if (species.toUpperCase().contains(s)) { int totalCount = counter.get(s) + count; counter.put(s, totalCount); isSpeciesOfInterest = true; } return isSpeciesOfInterest; } private String addReportInfo(String reportLine, String[] speciesList, Map counter) { String result = reportLine; for (String s:speciesList) result += String.format(";%d", counter.get(s)); return result; } }