Java Read a csv file and process the data

Click For Summary
The code snippet provided defines a method called `fromFile` that reads data from a specified file to create a `Region` object. It begins by reading the file's contents into a list of strings, with the first line serving as headers that are split into an array. A mapping of header names to their respective column indices is created for easy access. The method then processes each subsequent line, extracting relevant data such as province name, municipality name, and municipality altitude. It uses this information to create or retrieve `Municipality` objects. Additionally, it handles mountain hut data, either using a specified altitude or defaulting to a category and number of beds. The overall structure of the code is noted to be readable, with clear variable naming and logical flow, despite the author's self-identified rustiness in Java.
DottZakapa
Messages
239
Reaction score
17
TL;DR
Hi i need some help in this portion of code, i have no clue what it does... in particular the highlighted portion
[CODE lang="java" highlight="3-17"]public static Region fromFile(String name, String file) {
Region r = new Region(name);
List<String> lines = readData(file);

String[] headers = lines.remove(0).split(";");
Map<String, Integer> h2i = new HashMap<>();
for (int i=0; i<headers.length; i++) {
h2i.put(headers, i);
}

lines.forEach(l -> {
String[] rows = l.split(";");

String provinceName = rows[h2i.get("Province")];
String municipalityName = rows[h2i.get("Municipality")];
Integer municipalityAltitude = Integer.parseInt(rows[h2i.get("MunicipalityAltitude")]);

Municipality municipality = r.createOrGetMunicipality(municipalityName,
provinceName, municipalityAltitude);

String mh_name = rows[h2i.get("Name")];
String altitude = rows[h2i.get("Altitude")];
String category = rows[h2i.get("Category")];
Integer bedsNumber = Integer.parseInt(rows[h2i.get("BedsNumber")]);


if (altitude.equals("")) {
r.createOrGetMountainHut(mh_name, category, bedsNumber, municipality);
} else {
r.createOrGetMountainHut(mh_name, Integer.parseInt(altitude), category, bedsNumber, municipality);
}
});

return r;
}[/CODE]
 
Technology news on Phys.org
My Java is a bit rusty. But the code looks very readable to me. This is what it does:
  1. Read the data from a file into an array of strings, where each entry represents a single line in the data file.
  2. Extract the first line (I assume that is what Array.remove(0) does) and split it into an array of semicolon-separated header entries.
  3. Create a map M : "header entry" -> "column index" for future use
  4. For each line in the remaining lines:
    1. Get the array of semicolon-separated entries (counter-intuitively, the array is called "rows")
    2. Get the index of the column with the header "Province", read the entry at this location, and store it in the variable "provinceName".
    3. Do the equivalent for municipalityName and municipalityAltitude (in the 2nd case also convert the string to an integer)
    4. (Do some other stuff in the part of the code that is not highlighted)
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
11K
  • · Replies 6 ·
Replies
6
Views
11K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 10 ·
Replies
10
Views
25K
  • · Replies 8 ·
Replies
8
Views
10K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
4K