feature(domain): CSVReader angepasst

pull/3/head
dustineversmann 2025-01-06 22:41:15 +01:00
parent 1a32962ba8
commit 92bc8247e4
1 changed files with 37 additions and 19 deletions

View File

@ -4,36 +4,54 @@ import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class CSVReader { public class CSVReader {
/** public PuzzleData readPuzzleWithSolution(String csvFile) throws IOException {
* Liest die CSV-Datei ein und gibt die enthaltenen Werte als zweidimensionales int-Array zurück. List<int[]> puzzleRows = new ArrayList<>();
* List<int[]> solutionCoordinates = new ArrayList<>();
* @param csvFile Pfad zur CSV-Datei
* @return zweidimensionales Array, das die Zahlen aus der CSV enthält
* @throws IOException wenn ein Fehler beim Lesen auftritt
*/
public int[][] readCsvToIntArray(String csvFile) throws IOException {
ArrayList<int[]> rows = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) { try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
String line; String line;
boolean inSolutionPart = false;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] tokens = line.split(","); line = line.trim();
int[] row = new int[tokens.length]; if (line.isEmpty()) {
for (int i = 0; i < tokens.length; i++) { continue;
row[i] = Integer.parseInt(tokens[i].trim()); }
if (line.startsWith("//Lösung") || line.startsWith("//")) {
inSolutionPart = true;
continue;
}
if (!inSolutionPart) {
// Puzzle-Teil
String[] tokens = line.split(",");
int[] rowValues = new int[tokens.length];
for (int i = 0; i < tokens.length; i++) {
rowValues[i] = Integer.parseInt(tokens[i].trim());
}
puzzleRows.add(rowValues);
} else {
// Lösungsteil
String[] coords = line.split(",");
if (coords.length == 2) {
int row = Integer.parseInt(coords[0].trim());
int col = Integer.parseInt(coords[1].trim());
solutionCoordinates.add(new int[]{row, col});
}
} }
rows.add(row);
} }
} }
// ArrayList in 2D-Array umwandeln // puzzleRows -> 2D-Array
int[][] result = new int[rows.size()][]; int[][] puzzleArray = new int[puzzleRows.size()][];
for (int i = 0; i < rows.size(); i++) { for (int i = 0; i < puzzleRows.size(); i++) {
result[i] = rows.get(i); puzzleArray[i] = puzzleRows.get(i);
} }
return result;
return new PuzzleData(puzzleArray, solutionCoordinates);
} }
} }