From d7dbffeff828d2b6227a1ffeb14177e86d71f289 Mon Sep 17 00:00:00 2001 From: 3011357 <3011357@stud.hs-mannheim.de> Date: Tue, 9 Jan 2024 13:38:45 +0100 Subject: [PATCH] =?UTF-8?q?readCSV()=20Methode=20erg=C3=A4nzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../informatik/spreadsheet/Spreadsheet.java | 61 +++++++++++++++---- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 8484eaa..6585b36 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -1,12 +1,17 @@ package de.hs_mannheim.informatik.spreadsheet; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; + /** * A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim. * One aspect worth mentioning is that it only supports long numbers, not doubles. @@ -21,15 +26,17 @@ public class Spreadsheet { * @param rows number of rows * @param cols number of columns */ - public Spreadsheet(int rows, int cols) { + public Spreadsheet(int rows, int cols) throws IllegalArgumentException { - // TODO limit the maximum size on 99 (1..99) rows and 26 (A..Z) columns + if(rows < 1 || rows > 99 || cols < 1 || cols > 26){ + throw new IllegalArgumentException("Range in rows 1-99; Range in cols 1-26"); + }else { + cells = new Cell[rows][cols]; - cells = new Cell[rows][cols]; - - for (int r = 0; r < rows; r++) - for (int c = 0; c < cols; c++) - cells[r][c] = new Cell(); + for (int r = 0; r < rows; r++) + for (int c = 0; c < cols; c++) + cells[r][c] = new Cell(); + } } // ----- @@ -75,13 +82,41 @@ public class Spreadsheet { * A method for reading in data from a CSV file. * @param path The file to read. * @param separator The char used to split up the input, e.g. a comma or a semicolon. - * @param startCellName The upper left cell where data from the CSV file should be inserted. * @exception IOException If path does not exist. */ - public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException { - // TODO: implement this - } + public void readCsv(String path, String separator) throws FileNotFoundException { + ArrayList lines = new ArrayList<>(); + Scanner scan = new Scanner(new File(path)); + + while (scan.hasNextLine()) { + lines.add(scan.nextLine()); + } + + String[] linesArray = lines.toArray(new String[lines.size()]); + ArrayList values = new ArrayList<>(); + for(int i = 0; i < linesArray.length; i++) { + //System.out.println(linesArray[i]); + String[] zwischenSchritt = linesArray[i].split(separator); + for (int j = 0; j < zwischenSchritt.length; j++) { + System.out.println(zwischenSchritt[j]); + if(zwischenSchritt[j].startsWith("=")){ + values.add(new int[]{i, j}); + }else{ + this.put(i, j, zwischenSchritt[j]); + } + + } + for(int[] cell : values){ + String[] value = linesArray[cell[0]].split(","); + String formula = value[cell[1]]; + this.put(cell[0], cell[1], formula); + } + scan.close(); + } + + + } /** * A method for saving data to a CSV file. * @param path The file to write. @@ -307,8 +342,8 @@ public class Spreadsheet { /** * This method calculates the result of a "normal" algebraic expression. It only needs to support * expressions like =B4 or =2+A3-B2, i.e. only with int numbers and other cells and with plus, - * minus, times, split only. An expression always starts with either a number or a cell name. If it - * continues, it is guaranteed that this is followed by an operator and either a number or a + * minus, times, split only. An expression always starts with either a number or a cell name. If it + * continues, it is guaranteed that this is followed by an operator and either a number or a * cell name again. It is NOT required to implement dot before dash or parentheses in formulas. * @param formula The expression to be evaluated. * @return The result calculated.