-1

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
}
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Zach Rieck
  • 419
  • 1
  • 4
  • 23
  • *"is it possible/ practical to store this output into arrays so I can plot it?"* Why can't you plot it from the "Book" objects you have? What arrays / format do you want the data to be, to make "plotting" easier? – Andreas Mar 25 '20 at 01:27
  • 3
    *FYI:* Parallel arrays is a very bad idea. Use single array of objects. Which you already have in the form of a `List`, so ... What is it again you're trying to accomplish? – Andreas Mar 25 '20 at 01:31
  • @Andreas Parallel arrays is exactly what I'm asking about (sorry, should've used that term- I'll edit my question). Can you elaborate on why this is a bad idea? Feel free to respond as an answer, this answers my question. – Zach Rieck Mar 25 '20 at 17:11
  • See this answer to [Why use parallel arrays in Java?](https://stackoverflow.com/a/5347828/5221149), or this [Wikipedia article](https://en.wikipedia.org/wiki/Parallel_array#Pros_and_cons), and if you never have to rearrange the data, e.g. sorting it, you'd learn for sure why it's a bad idea. – Andreas Mar 25 '20 at 20:03

1 Answers1

1

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.

Zach Rieck
  • 419
  • 1
  • 4
  • 23