import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; /* Reads data from a specific section of a text file into lists mapped by column headers. path: The path to the text file. start_string: The string that marks the beginning of the data section. end_string: The string that marks the end of the data section. return: Map where keys = column headers and values = lists of data. */ public class flowNetInput { public static void main(String[] args) { // Define the file path and section markers String file_path = "flow_net_input.txt"; String start_string = "[FLOW RESISTORS BEGIN]"; String end_string = "[FLOW RESISTORS END]"; // Call the method to read and process the data Map> column_data = readSectionData(Path.of(file_path), start_string, end_string); // Display the results if (!column_data.isEmpty()) { System.out.println("Data successfully read for the following columns:"); for (String header : column_data.keySet()) { System.out.println("--- Column: " + header + " ---"); System.out.println(column_data.get(header)); } } else { System.out.println("No data found within the specified section."); } } public static Map> readSectionData(Path file_path, String start_string, String end_string) { Map> data_map = new HashMap<>(); boolean inSection = false; List headers = null; // Regex for one or more spaces, tabs, or commas String delimiterRegex = "[\\s,]+"; try (BufferedReader reader = new BufferedReader(new FileReader(file_path.toFile()))) { String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (line.equals(start_string)) { inSection = true; // Skip any lines between the start marker and the headers, if necessary, // or assume the next line is the header. continue; } if (line.equals(end_string)) { inSection = false; break; // Stop reading after the end marker } if (inSection) { if (headers == null) { // This is the header line. Split and initialize lists. headers = Arrays.asList(line.split(delimiterRegex)); for (String header : headers) { data_map.put(header.trim(), new ArrayList<>()); } } else { // This is a data line. Split and store in the respective lists. String[] values = line.split(delimiterRegex); for (int i = 0; i < headers.size() && i < values.length; i++) { data_map.get(headers.get(i).trim()).add(values[i].trim()); } } } } } catch (IOException e) { e.printStackTrace(); } return data_map; } /* Converts the values of a Map into a List (or Vector) data_map = The input map return = A List containing all values */ public static List convertKeysToVector(Map data_map) { return new ArrayList<>(data_map.keySet()); } public static List convertValuesToVector(Map data_map) { return new ArrayList<>(data_map.values()); } }