March 29th, 2013, 11:57 AM
Reading csv into array to perform actions on array
Hi Everyone. This is my first post and it is a big one />. I am working on a Java assignment that is tied into the next three + final assignments for this course. This assignment is due tomorrow by midnight and I am stumped. This is an online course and the text we were required to get is outdated. I sent emails to my prof asking why every exercise in the book gave errors. His response was "Java has changed since the book came out, skip those". So I read on only only to find out there are no topics covering arrays or reading csv files. The requirements for the assignment are as follows:
Download stock data from yahoo (historical prices) in a csv file.
Create a file ReadFiles.java. This is the file that will read in the data from your csv file. Your ReadFiles.java class requires the following methods:
Method: check to see if the file exists
Method: find number of rows in csv file
Method: Converts the csv file to an array. You will need to use java.util.StringTokenizer
Method: PrintArray For testing purposes
Method: Return array using a get method
Create a file DataAnalyzer.java. This file will be used to call the methods in ReadFiles.java.
I am not sure on how I go about doing this. I have been able to do the following:
Now this does provide the information I need:
public class ReadFiles
public static int numberOfRows;
public static int rowNumber = 0;
public static int columnNumber = 0;
public static String columnTitle;
public static void main(String args)
Scanner kb = new Scanner (System.in);
System.out.print("Enter the file name >> ");
fileName = kb.nextLine();
File f = new File("D:\\Java\\Assignment 3\\" + fileName);
System.out.print("File exists. \n");
fileName="D:\\Java\\Assignment 3\\" + fileName;
BufferedReader br = new BufferedReader(new FileReader(fileName));
StringTokenizer st = null;
while((fileName = br.readLine()) != null)
st = new StringTokenizer(fileName, ",");
System.out.println("Row " + rowNumber +
", Column " + columnNumber
+ ", Data : "+ st.nextToken());
columnNumber = 0;
catch (FileNotFoundException e)
catch (IOException e)
What I am not sure about is if this code be used in the DataAnalyzer.java file? Do I need to create the array in the ReadFiles.java? How do I go about putting this information into an array?
Row 1, Column 1, Data : 30/12/2011
Row 1, Column 2, Data : 642.02
Row 1, Column 3, Data : 646.76
Row 1, Column 4, Data : 642.02
Row 1, Column 5, Data : 645.9
Row 1, Column 6, Data : 1782300
Row 1, Column 7, Data : 645.9
Row 2, Column 1, Data : 29/12/2011
Any help on this would be much appreciated.
March 29th, 2013, 08:01 PM
I'm assuming you want the data to go into a 2 dim array.
Do you know how many rows and columns the array needs?
To fill the array:
read each line, split the line at the commas and store each token into the next column on the current row. Advance to the next row and do it again.
Accessing two dim array elements:
March 30th, 2013, 12:53 PM
Yes 2dim. The rows would have to be dynamic. The file will change each day. Columns would be 7
March 30th, 2013, 12:57 PM
Arrays are fixed to the size given them when defined. If you know the max possible number of rows, then you could define it larger to be sure and use a counter to keep track of the number of rows filled.
Or make two passes through the data to count the rows before defining the array.