1
0
Fork 0

save/read am ende und anfang

main
Jan 2024-01-07 16:52:10 +01:00
parent e5d531a7a4
commit 883c36c929
2 changed files with 29 additions and 19 deletions

View File

@ -1,6 +1,5 @@
package de.hs_mannheim.informatik.spreadsheet;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
@ -17,18 +16,8 @@ public class Axel {
Spreadsheet spr = new Spreadsheet(10,6);
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
spr.put("A3", "123");
spr.put("B9", "=41+A2+10-2/3");
spr.put("B8", "200");
spr.put("B7", "200");
spr.put("B6", "5");
spr.put("B5", "5");
spr.put("B4", "24");
spr.put("B3", "24");
spr.put("B2", "28");
spr.put("B1", "24");
String filePath = "Axel/resources/zahlen.csv";
spr.readCsv(filePath,"A3");
System.out.println("Schreibe 'stop' zum anhalten.");
@ -63,8 +52,6 @@ public class Axel {
}
}while(true);
String filePath = "Axel/resources/zahlen.csv";
try (PrintWriter writer = new PrintWriter(new FileOutputStream(filePath))) {
spr.saveCsv(writer);
System.out.println("CSV file saved successfully.");

View File

@ -1,5 +1,6 @@
package de.hs_mannheim.informatik.spreadsheet;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
@ -7,6 +8,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
@ -86,13 +88,34 @@ 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 starCellName The upper left cell where data from the CSV file should be inserted.
* @param startCellName The upper left cell where data from the CSV file should be inserted.
* @return Nothing.
* @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 startCellName) throws FileNotFoundException {
Scanner csvIn = new Scanner(new File(path));
int index= 0;
while(csvIn.hasNextLine()){
String line =csvIn.nextLine();
String all="";
int filled=0;
for(int i = 0;i<line.length();i++) {
if(line.charAt(i)==',') {
if(!all.isBlank()) {
put(index,i-filled,all);
all="";
}
}else {
filled++;
all+=line.charAt(i);
}
}
index++;
}
csvIn.close();
}
/**