Commit 0f528cdd authored by edmundszagars's avatar edmundszagars
Browse files

Refactoring "getDataFromCSV" to use lists instead of 2d array

parent 6856d0d3
1, 2, 3, 4
1, 2, 3
1, 2, 3
1, 2, 3
......@@ -17,4 +16,5 @@
1, 2, 3
1, 2, 3
1, 2, 3
1, 2, 3
\ No newline at end of file
1, 2, 3
1, 2, 3, 4
\ No newline at end of file
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GetDataFromCSV {
private double[][] nDataArray;
private String nFileLocation = "data.csv";
//TODO: Make data array dynamic
public double[][] getDataFromCSV() {
nDataArray = new double[20][5];
Scanner scanner;
int cRow = 0;
String inputLine;
String nFileLocation = "data.csv";
try {
scanner = new Scanner(new BufferedReader(new FileReader(nFileLocation)));
while (scanner.hasNextLine()) {
inputLine = scanner.nextLine();
String[] inArray = inputLine.split(",");
for (int i = 0; i < inArray.length; i++) {
nDataArray[cRow][i] = Double.parseDouble(inArray[i]);
}
cRow++;
public ArrayList getDataFromCSV() throws FileNotFoundException {
List<ArrayList<Double>> nListOfValues = new ArrayList<>();
Scanner scanner = new Scanner(new BufferedReader(new FileReader(nFileLocation)));
while (scanner.hasNextLine()){
String nLine = scanner.nextLine();
String[] inArray = nLine.split(",");
ArrayList<Double> list = new ArrayList<>();
for (String anInArray : inArray) {
list.add(Double.parseDouble(anInArray));
}
if(list.size() != 0){
nListOfValues.add(list);
}
} catch (FileNotFoundException | ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
return nDataArray;
return (ArrayList) nListOfValues;
}
public void setnFileLocation(String nFileLocation) {
this.nFileLocation = nFileLocation;
}
public String getnFileLocation() {
return nFileLocation;
}
}
......@@ -2,9 +2,9 @@ import java.io.FileNotFoundException;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
public static void main(String[] args) throws FileNotFoundException {
GetDataFromCSV get = new GetDataFromCSV();
double[][] resultArray = get.getDataFromCSV();
System.out.println(Arrays.deepToString(resultArray));
for (int i = 0 ; i < get.getDataFromCSV().size();i++)
System.out.println(get.getDataFromCSV().get(i));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment