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)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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