Java Read a csv file and process the data

Click For Summary
SUMMARY

The discussion focuses on a Java method named fromFile that reads a CSV file and processes its data into a Region object. The method utilizes a semicolon as a delimiter to parse the file, creating a mapping of header names to their respective column indices for efficient data retrieval. It constructs Municipality and MountainHut objects based on the parsed data, handling both altitude and bed number attributes. The code is noted for its readability and effective use of data structures.

PREREQUISITES
  • Java programming language proficiency
  • Understanding of CSV file structure and parsing
  • Familiarity with Java Collections Framework, specifically Map and List
  • Basic knowledge of object-oriented programming concepts in Java
NEXT STEPS
  • Explore Java's BufferedReader for efficient file reading
  • Learn about Java's Stream API for processing collections
  • Investigate error handling in file I/O operations in Java
  • Study the implementation of custom data structures in Java
USEFUL FOR

Java developers, data engineers, and anyone involved in processing CSV data files in Java applications.

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)
 
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 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 6 ·
Replies
6
Views
11K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 10 ·
Replies
10
Views
26K
  • · Replies 8 ·
Replies
8
Views
10K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
4K