Reading in a *.csv file and loading the data into an Array

Reply

Join Date: Jan 2005
Posts: 31
Reputation: AQWst is an unknown quantity at this point 
Solved Threads: 0
AQWst AQWst is offline Offline
Light Poster

Reading in a *.csv file and loading the data into an Array

 
0
  #1
Jan 20th, 2005
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.  
  2. 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
  3. 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Reading in a *.csv file and loading the data into an Array

 
0
  #2
Jan 22nd, 2005
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.

  1.  
  2. String[24][24] numbers;
  3.  
  4. File file = new File("something.csv");
  5.  
  6. BufferedReader bufRdr = new BufferedReader(new FileReader(file));
  7. String line = null;
  8. int row = 0;
  9. int col = 0;
  10.  
  11. //read each line of text file
  12. while((line = bufRdr.readLine()) != null)
  13. {
  14. StringTokenizer st = new StringTokenizer(line,",");
  15. while (st.hasMoreTokens())
  16. {
  17. //get next token and store it in the array
  18. numbers[row][col] = st.nextToken();
  19. col++;
  20. }
  21. row++;
  22. }
  23.  
  24. //close the file
  25. bufRdr.close();
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 31
Reputation: AQWst is an unknown quantity at this point 
Solved Threads: 0
AQWst AQWst is offline Offline
Light Poster

Re: Reading in a *.csv file and loading the data into an Array

 
0
  #3
Jan 22nd, 2005
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).

  1. String [][] numbers = new String [24][24];
  2.  
  3. File file = new File("Currency Exchange Rates.csv");
  4.  
  5. BufferedReader bufRdr = new BufferedReader(new FileReader(file));
  6. String line = null;
  7. int row = 0;
  8. int col = 0;
  9.  
  10. //read each line of text file
  11. while((line = bufRdr.readLine()) != null && row < 24)
  12. {
  13. StringTokenizer st = new StringTokenizer(line,",");
  14. while (st.hasMoreTokens())
  15. {
  16. //get next token and store it in the array
  17. numbers[row][col] = st.nextToken();
  18. col++;
  19. }
  20. col = 0;
  21. row++;
  22. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5
Reputation: chackboom has a little shameless behaviour in the past 
Solved Threads: 2
chackboom chackboom is offline Offline
Newbie Poster

Re: Reading in a *.csv file and loading the data into an Array

 
-1
  #4
Aug 28th, 2009
Originally Posted by Phaelax View Post
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.

  1.  
  2. String[24][24] numbers;
  3.  
  4. File file = new File("something.csv");
  5.  
  6. BufferedReader bufRdr = new BufferedReader(new FileReader(file));
  7. String line = null;
  8. int row = 0;
  9. int col = 0;
  10.  
  11. //read each line of text file
  12. while((line = bufRdr.readLine()) != null)
  13. {
  14. StringTokenizer st = new StringTokenizer(line,",");
  15. while (st.hasMoreTokens())
  16. {
  17. //get next token and store it in the array
  18. numbers[row][col] = st.nextToken();
  19. col++;
  20. }
  21. row++;
  22. }
  23.  
  24. //close the file
  25. bufRdr.close();
String field[] = line.split(",");
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,176
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Reading in a *.csv file and loading the data into an Array

 
0
  #5
Aug 29th, 2009
@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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: rmkapil is an unknown quantity at this point 
Solved Threads: 0
rmkapil rmkapil is offline Offline
Newbie Poster
 
0
  #6
21 Days Ago
corrections in fossilized threads is certainly needed becuase those are still the source of information for many.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC