forked from hummel/PR1-Spreadsheet
readCSV() Methode ergänzt
parent
c980006f02
commit
d7dbffeff8
|
@ -1,12 +1,17 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
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.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim.
|
* 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.
|
* 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 rows number of rows
|
||||||
* @param cols number of columns
|
* @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++)
|
||||||
for (int r = 0; r < rows; r++)
|
cells[r][c] = new Cell();
|
||||||
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.
|
* A method for reading in data from a CSV file.
|
||||||
* @param path The file to read.
|
* @param path The file to read.
|
||||||
* @param separator The char used to split up the input, e.g. a comma or a semicolon.
|
* @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.
|
* @exception IOException If path does not exist.
|
||||||
*/
|
*/
|
||||||
public void readCsv(String path, char separator, String startCellName) throws FileNotFoundException {
|
public void readCsv(String path, String separator) throws FileNotFoundException {
|
||||||
// TODO: implement this
|
|
||||||
}
|
|
||||||
|
|
||||||
|
ArrayList<String> 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<int[]> 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.
|
* A method for saving data to a CSV file.
|
||||||
* @param path The file to write.
|
* @param path The file to write.
|
||||||
|
|
Loading…
Reference in New Issue