Java Read a csv file and process the data

AI Thread 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 Summary
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)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
2K
Replies
4
Views
11K
Replies
6
Views
10K
Replies
5
Views
3K
Replies
6
Views
3K
Replies
10
Views
25K
Replies
8
Views
10K
Replies
1
Views
3K
Back
Top