I have a Java program I have written in Eclipse with the goal of taking data values stored in Excel and putting those into a correlation plot for hypothesis testing using the Nebula plug in. I am using buffered reader to import my data, and I have been able to get the data I want to return to the console using encapsulation and constructors. My question: is it possible/ practical to store this output into arrays so I can plot it? My thought is to initialize the arrays and then store the output via a method somehow, but I'm stuck on how to do it....
EDIT: I should clarify- I am asking about transferring my object into parallel arrays. Is this good practice or not? And if so, how can this be done most efficiently?
Some code (it's long, but I don;t want to leave anything critical out):
public class CovidPlots {
public static void main(String[] args) {
//Data source
List<Book> stocks = readBooksFromCSV("COVID_DJI.csv");
for (Book b : stocks) {
System.out.println(b);
}
private static List<Book> readBooksFromCSV(String fileName) {
List<Book> stocks = new ArrayList<>();
Path pathToFile = Paths.get("C:\\Users\\zrr81\\Downloads\\COVID-19_Analysis\\COVID_DJI.csv");
//Try/catch IO exception for input file stream
try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) {
//Initialize counter to skip header
int count = 0;
String line = br.readLine();
while(line != null) {
//Tell the reader to split the text when it hits the delimiter
String[] attributes = line.split(",");
Book book = createBook(attributes);
stocks.add(book);
count ++;
line = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return stocks;
}
//Create output arrays
String[] days;
double [] openers;
double[] closers;
//Import data into array "Book"
private static Book createBook(String[] metadata) {
String day = metadata[0];
double opener = Double.parseDouble(metadata[1]);
double closer = Double.parseDouble(metadata[4]);
return new Book(day, opener, closer);
}
}
//Book class file (separate file in my package)
class Book {
//Define spreadsheet "Book"
private String date;
private double opener;
private double closer;
// All-args constructor, getters, setters and toString()
// left out for brevity
}
I've done a bunch more research, and I think I found a way to easily build these into arrays. Posting here in case someone else has the same question:
//Create arrays that can hold output
double[] opening;
double[] closing;
int len = stocks.size();
opening = new double[len];
closing = new double[len];
int j = 0;
for (Book x : stocks) {
double o_value = x.getOpen();
double c_value = x.getClose();
opening[j] = o_value;
closing[j] = c_value;
j++;
}
This pulls the constructor values even though they're private because it utilizes a for each loop with an object of the same type as the ArrayList. This creates parallel arrays with each holding the data I need to plot. Still don't know if this is best practice given I've read different things on parallel arrays, but it solves my issue.