From 0c46be39b4310bc2d6512d91bf84470126f28a92 Mon Sep 17 00:00:00 2001 From: 3010564 <3010564@stud.hs-mannheim.de> Date: Sun, 7 Jan 2024 21:13:17 +0100 Subject: [PATCH] =?UTF-8?q?Kleines=20Men=C3=BC=20erstellt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zeilen und Spaltenbegrenzung csvReader erstellt --- .../informatik/spreadsheet/Spreadsheet.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 53cd48e..7abba47 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -1,8 +1,11 @@ package de.hs_mannheim.informatik.spreadsheet; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -26,10 +29,11 @@ public class Spreadsheet { cells = new Cell[rows][cols]; - for (int r = 0; r < 99; r++) - for (int c = 0; c < 26; c++) + for (int r = 0; r < rows; r++) + for (int c = 0; c < cols; c++) cells[r][c] = new Cell(); //Fehlermeldung wenn es über 99 Zeilen und 26 Spalten sind + //Oben genanntes bringt nichts. Bugfix durch zurücksetzen auf vorherigen Wert. Fehlermeldung unnötig durch if Abfrage. } // ----- @@ -79,8 +83,35 @@ public class Spreadsheet { */ public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException { // TODO: implement this + ArrayList lines = readFile(path); + String[] seperateLines = new String[lines.size()]; + int startingCellRow = getRow(startCellName); + int startingCellCol = getCol(startCellName); + String[][] quickSave = new String[cells.length - startingCellRow][cells[0].length - startingCellCol]; + for (int i = 0; i < seperateLines.length; i++) { + quickSave[i] = lines.get(i).split(separator+""); //Seperator erstellt + for (int j = 0; j < quickSave[i].length; j++) { + put(startingCellRow+i,startingCellCol+j,quickSave[i][j]); + } + } } + public static ArrayList readFile(String path) throws FileNotFoundException { + ArrayList lines = new ArrayList<>(); + Scanner sc = new Scanner(new File(path)); + + while (sc.hasNextLine()) { + lines.add(sc.nextLine()); + } + + sc.close(); + + return lines; + } + + + + /** * A method for saving data to a CSV file. * @param path The file to write.