0

I am looking at the performance of a particular stock over a period of 30 days. I downloaded the data from Yahoo finance which appears as a CSV file. If I would like to create a new column in my dataset to show the daily percentage change between open and close using java refer to column H as where the output should appear, how should I do so?

Thanks in advance!

avajhelp
  • 25
  • 3

2 Answers2

0

You can just edit your file line-by-line and add a separator character using the concat() function.

Open the file with a FileReader or BufferedReader and then start parsing.

Official Javadoc: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#concat(java.lang.String)

Please see also this.

Community
  • 1
  • 1
Thecave3
  • 762
  • 12
  • 27
0

With OpenCSV: http://opencsv.sourceforge.net/

public class OpenCSVTest {

    public static void main(String[] args) {

        StringWriter target = new StringWriter();

        try(CSVReader reader = new CSVReader(new StringReader("my,test")); 
            CSVWriter writer = new CSVWriter(target);) {
            for(String[] line : reader) {
                String[] added = Arrays.copyOf(line, line.length + 1);
                added[added.length-1] = "addition";
                writer.writeNext(added);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        System.out.println(target.toString());
    }
}

You can replace the StringReader and StringWriter with your input and output. The reader will then iterate through each line in your csv for you. You can make a copy of the original line and add your new column and write it out into the target.

Breakdown of the code:

try(CSVReader reader = new CSVReader(new StringReader("my,test")); 
            CSVWriter writer = new CSVWriter(target);) { ... }

This is a try-with-resources block. It makes sure that the reader and writer are closed after they are done.

for(String[] line : reader) {
     String[] added = Arrays.copyOf(line, line.length + 1);
     added[added.length-1] = "addition";
     writer.writeNext(added);
}

This is a standard for loop. The CSVReader implements the Iterable interface, which enables us to use it in the for loop directly.

Arrays.copyOf(line, line.length + 1); is a function that creates a copy of the array passed to it, but with a new size. Because you want to add a column, we make a copy of the original array my,test and add 1 more space at the end of it, where we can then assign the new value to it by doing: added[added.length-1] = "addition";

Finally, we just pass that to the writer which will then correctly write the values into the target, in this case a StringWriter, in your case likely a file.

pandaadb
  • 6,306
  • 2
  • 22
  • 41
  • Hi, could you explain what the portion in between the try and catch means? Sorry I have very basic understanding of java – avajhelp Apr 05 '17 at 11:06
  • @avajhelp i have added a detailed explanation. let me know if something is unclear – pandaadb Apr 05 '17 at 11:18