From 43694e5845eb78dc964e95071e9b4aaa0bacfee9 Mon Sep 17 00:00:00 2001 From: 3012330 <3012330@stud.hs-mannheim.de> Date: Thu, 4 Jan 2024 20:49:03 +0100 Subject: [PATCH] Added the "readCsv" method. --- .../informatik/spreadsheet/Spreadsheet.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 4ed4cac..a9c85e2 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -1,10 +1,13 @@ 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.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.ArrayList; /** * A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim. @@ -121,6 +124,30 @@ public class Spreadsheet { */ public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException { // TODO: implement this + ArrayList lines = readFile(path); + String[] einzelnde_lines = new String[lines.size()]; + int row_startcell = getRow(startCellName); + int col_startcell = getCol(startCellName); + String[][] zwischenspeicher = new String[cells.length - row_startcell][cells[0].length - col_startcell]; + for (int i = 0; i < einzelnde_lines.length; i++) { + zwischenspeicher[i] = lines.get(i).split(separator+""); // +"" um aus dem Char ein String zu machen + for (int j = 0; j < zwischenspeicher[i].length; j++) { + put(row_startcell+i,col_startcell+j,zwischenspeicher[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; } /**