1
0
Fork 0

Kleines Menü erstellt

Zeilen und Spaltenbegrenzung

csvReader erstellt
main
Sebastian Tews 2024-01-07 21:13:17 +01:00
parent df6ff99a0c
commit 0c46be39b4
1 changed files with 33 additions and 2 deletions

View File

@ -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<String> 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<String> readFile(String path) throws FileNotFoundException {
ArrayList<String> 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.