DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Reading in a *.csv file and loading the data into an Array (http://www.daniweb.com/forums/thread17262.html)

AQWst Jan 20th, 2005 2:34 pm
Reading in a *.csv file and loading the data into an Array
 
I would like to know any advice for creating a program that will read in a *.csv file and load the data into a 24 x 24 array for further processing. I am including a total of 48 records, so that you can see how the data looks and what I'll be working with.

 
1.000000,0.767801,0.370782,1.887500,0.817261,0.120824,0.178702,1.329200,0.128401,0.022936,0.009620,0.263158,0.088230,0.702998,0.160940,0.607977,0.169635,0.000956,0.009970,0.147252,0.856678,0.031328,0.025595,0.000522
1.302420,1.000000,0.482914,2.458320,1.064420,0.157364,0.232745,1.731180,0.167232,0.029872,0.012529,0.342742,0.114913,0.915598,0.209611,0.791841,0.220936,0.001245,0.012985,0.191783,1.115750,0.040803,0.033336,0.000680

Phaelax Jan 22nd, 2005 10:59 am
Re: Reading in a *.csv file and loading the data into an Array
 
I don't know what a csv file is or what the structure is, but if its just a text a file with numbers like that, it's quite simple.

The only thing to be careful about in this code is that it assumes there won't be more than 24 elements, or tokens, in each line of text.

 
String[24][24] numbers;
 
File file = new File("something.csv");
 
BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;
 
//read each line of text file
while((line = bufRdr.readLine()) != null)
{
        StringTokenizer st = new StringTokenizer(line,",");
        while (st.hasMoreTokens())
        {
                //get next token and store it in the array
                numbers[row][col] = st.nextToken();
                col++;
        }
        row++;
}
 
//close the file
bufRdr.close();

AQWst Jan 22nd, 2005 12:42 pm
Re: Reading in a *.csv file and loading the data into an Array
 
Thanks for the help. I only had to change the program slightly with changes to the logic for it to work properly. (Please see my code to see what I did).

        String [][] numbers = new String [24][24];
 
        File file = new File("Currency Exchange Rates.csv");
 
        BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
        String line = null;
        int row = 0;
        int col = 0;
 
        //read each line of text file
        while((line = bufRdr.readLine()) != null && row < 24)
        {       
        StringTokenizer st = new StringTokenizer(line,",");
        while (st.hasMoreTokens())
        {
                //get next token and store it in the array
                numbers[row][col] = st.nextToken();
                col++;
        }
        col = 0;
        row++;
        }
:)

chackboom Aug 28th, 2009 9:05 pm
Re: Reading in a *.csv file and loading the data into an Array
 
Quote:

Originally Posted by Phaelax (Post 86160)
I don't know what a csv file is or what the structure is, but if its just a text a file with numbers like that, it's quite simple.

The only thing to be careful about in this code is that it assumes there won't be more than 24 elements, or tokens, in each line of text.

 
String[24][24] numbers;
 
File file = new File("something.csv");
 
BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;
 
//read each line of text file
while((line = bufRdr.readLine()) != null)
{
        StringTokenizer st = new StringTokenizer(line,",");
        while (st.hasMoreTokens())
        {
                //get next token and store it in the array
                numbers[row][col] = st.nextToken();
                col++;
        }
        row++;
}
 
//close the file
bufRdr.close();

String field[] = line.split(",");

stephen84s Aug 29th, 2009 11:26 am
Re: Reading in a *.csv file and loading the data into an Array
 
@chackboom

Am pretty sure the O.P. must have found a solution to it long long ago, please check the dates before posting on such fossilized threads.

rmkapil Oct 31st, 2009 4:48 pm
corrections in fossilized threads is certainly needed becuase those are still the source of information for many.


All times are GMT -4. The time now is 10:13 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC