2

I am trying to add data from a CSV file into a list. At the moment I have this code below, however the application closes whenever I try to run it.

private HashMap<String, GeoLocation> loadLocationData() {
    String csvFile = "C:\\Users\\MyName\\Documents\\MyApplication\\app\\src\\main\\res\\raw\\geo_locations.csv";
    String line = "";
    String cvsSplitBy = ",";

    try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

        while ((line = br.readLine()) != null) {
            String[] cityloc = line.split(cvsSplitBy);
            locations.put(cityloc[0], new GeoLocation(cityloc[0], Double.parseDouble(cityloc[1]), Double.parseDouble(cityloc[2]), TimeZone.getTimeZone(cityloc[3])));
       }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return locations;
}

I have already asked about reading CSV files and was given a list of other questions on it, however, after working on it I have not been able to resolve my problem here.

The basic gist of what I need to do, is get that list from the CSV file, and create a list from it that I can add to "locations".

  • Possible duplicate of [Get and Parse CSV file in android](http://stackoverflow.com/questions/5360628/get-and-parse-csv-file-in-android) ... I'm not sure that `C:` is meaningful on an Android device. – Tim Biegeleisen Oct 31 '16 at 06:51
  • What happens if CSV is malformed? You dont have all args in place (empty line)? Double or TimeZone is incorrectly formatted? Is your fuction save to handle that? – Witold Kaczurba Oct 31 '16 at 06:51
  • Your Android device/emulator does not have a C: drive. – Mike M. Oct 31 '16 at 06:52
  • "C:\\Users\\MyName\\Documents\\MyApplication\\app\\src\\main\\res\\raw\\geo_locations.csv" Is obviously a path on your development machine. Even if you run that app in an emulator, you need to use a path that actually exists on the emulated device. You may take a look into the Android developer website that deals with application resources and how to access them. https://developer.android.com/guide/topics/resources/providing-resources.html – Fildor Oct 31 '16 at 07:57

2 Answers2

0

Don't try to parse CSV by hand as there are many corner cases around this format (quote escapes, etc). Use univocity-parsers and it should work just fine.

Try this code:

    CsvParserSettings config = new CsvParserSettings();
    //configure what you need by hand, or just do this:
    config.detectFormatAutomatically();

    CsvRoutines csv = new CsvRoutines(config);

    File input = new File("C:\\Users\\MyName\\Documents\\MyApplication\\app\\src\\main\\res\\raw\\geo_locations.csv");

    Map<String, GeoLocation> locations = new LinkedHashMap<String, GeoLocation>();
    for(GeoLocation geo : csv.iterate(GeoLocation.class, input)){
        locations.put(geo.city, geo);
    }

My GeoLocation implementation:

public class GeoLocation{
    @Parsed(index = 0)
    String city;
    @Parsed(index = 1)
    Double longitude;
    @Parsed(index = 2)
    Double latitude;
    @Parsed(index = 3)
    TimeZone timeZone;

    public void setTimeZone(String timeZone){
        this.timeZone = TimeZone.getTimeZone(timeZone);
    }
}
Jeronimo Backes
  • 6,141
  • 2
  • 25
  • 29
0

change your file path as @Fildor said, for example "assets"

ray
  • 51
  • 7