#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    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:

    Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.StringTokenizer;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    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);
    		String fileName;
    		System.out.print("Enter the file name >> ");
    		fileName = kb.nextLine();
    		File f = new File("D:\\Java\\Assignment 3\\" + fileName);
    		if(f.exists())
    		{
    			System.out.print("File exists. \n");
    		}
    		fileName="D:\\Java\\Assignment 3\\" + fileName;
    		try 
    		{
    			BufferedReader br = new BufferedReader(new FileReader(fileName));
    			StringTokenizer st = null;
    
    			while((fileName = br.readLine()) != null)
    			{
    				rowNumber++;
    				numberOfRows++;
    		 
    				st = new StringTokenizer(fileName, ",");
    		 
    				while(st.hasMoreTokens())
    				{
    						columnNumber++;
    						System.out.println("Row " + rowNumber + 
    									", Column " + columnNumber 
    								+ ", Data : "+ st.nextToken());
    						
    				}
    				columnNumber = 0;
    			}
    		} 		
    		catch (FileNotFoundException e) 
    		{
    			e.printStackTrace();
    		} 
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		}
    	}
    }
    Now this does provide the information I need:

    Code:
    File exists. 
    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
    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?

    Any help on this would be much appreciated.

    Thanks
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    How do I go about putting this information into an array?
    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:
    twoDimArray[row][col]
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    Yes 2dim. The rows would have to be dynamic. The file will change each day. Columns would be 7
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    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.

IMN logo majestic logo threadwatch logo seochat tools logo