943,463 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 195497
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 20th, 2005
0

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

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
AQWst is offline Offline
31 posts
since Jan 2005
Jan 22nd, 2005
0

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.

Java Syntax (Toggle Plain 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();
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jan 22nd, 2005
0

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).

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
AQWst is offline Offline
31 posts
since Jan 2005
Aug 28th, 2009
-1

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

Click to Expand / Collapse  Quote originally posted by Phaelax ...
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.

Java Syntax (Toggle Plain 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(",");
Reputation Points: 3
Solved Threads: 2
Newbie Poster
chackboom is offline Offline
8 posts
since Apr 2006
Aug 29th, 2009
0

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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Oct 31st, 2009
-1
Re: Reading in a *.csv file and loading the data into an Array
corrections in fossilized threads is certainly needed becuase those are still the source of information for many.
Reputation Points: 9
Solved Threads: 0
Newbie Poster
rmkapil is offline Offline
1 posts
since Oct 2009
Jul 21st, 2010
0

I am working on a class and wanted to share this with you

The previous example breaks after col 25 because the array size reaches it limit.Here is a solution that does not depend on the arraysize

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.Reader;
  8. import java.util.StringTokenizer;
  9.  
  10.  
  11. public class Main {
  12.  
  13. /**
  14. * @param args
  15. */
  16. public static void main(String[] args) {
  17. // TODO Auto-generated method stub
  18. String fileName="C:/Documents and Settings/tntadmin/workspace2/a.csv";
  19. try {
  20. BufferedReader br = new BufferedReader( new FileReader(fileName));
  21. String strLine = null;
  22. StringTokenizer st = null;
  23. int lineNumber = 0, tokenNumber = 0;
  24.  
  25. while( (fileName = br.readLine()) != null)
  26. {
  27. lineNumber++;
  28.  
  29. //break comma separated line using ","
  30. st = new StringTokenizer(fileName, ",");
  31.  
  32. while(st.hasMoreTokens())
  33. {
  34. //display csv values
  35. tokenNumber++;
  36. System.out.println("Line # " + lineNumber +
  37. ", Token # " + tokenNumber
  38. + ", Token : "+ st.nextToken());
  39. }
  40.  
  41. //reset token number
  42. tokenNumber = 0;
  43.  
  44. }
  45. }
  46.  
  47.  
  48.  
  49.  
  50. catch (FileNotFoundException e) {
  51. // TODO Auto-generated catch block
  52. e.printStackTrace();
  53. } catch (IOException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. }
  58.  
  59. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darkknightgaury is offline Offline
4 posts
since Oct 2006
Jul 21st, 2010
0

Here is an update in the class that I am developing today july 21 2010

Here is my pimp code


Java Syntax (Toggle Plain Text)
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.StringTokenizer;
  7.  
  8.  
  9. public class CSVFileReader {
  10.  
  11. String fileName;
  12.  
  13. ArrayList <String>storeValues = new ArrayList<String>();
  14. public CSVFileReader(String FileName)
  15. {
  16. this.fileName=FileName;
  17. }
  18.  
  19. public void ReadFile()
  20. {
  21. try {
  22. //storeValues.clear();//just in case this is the second call of the ReadFile Method./
  23. BufferedReader br = new BufferedReader( new FileReader(fileName));
  24.  
  25. StringTokenizer st = null;
  26. int lineNumber = 0, tokenNumber = 0;
  27.  
  28. while( (fileName = br.readLine()) != null)
  29. {
  30. lineNumber++;
  31. System.out.println(fileName);
  32. storeValues.add(fileName);
  33. //break comma separated line using ","
  34. st = new StringTokenizer(fileName, ",");
  35.  
  36. while(st.hasMoreTokens())
  37. {
  38.  
  39. System.out.println("Line # " + lineNumber +
  40. ", Token # " + tokenNumber
  41. + ", Token : "+ st.nextToken());
  42.  
  43. }
  44.  
  45. //reset token number
  46. tokenNumber = 0;
  47.  
  48. }
  49. } catch (FileNotFoundException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. } catch (IOException e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56.  
  57.  
  58. }
  59.  
  60.  
  61.  
  62. //mutators and accesors
  63. public void setFileName(String newFileName)
  64. {
  65. this.fileName=newFileName;
  66. }
  67. public String getFileName()
  68. {
  69. return fileName;
  70. }
  71. public ArrayList getFileValues()
  72. {
  73. return this.storeValues;
  74. }
  75. public void displayArrayList()
  76. {
  77. for(int x=0;x<this.storeValues.size();x++)
  78. {
  79. System.out.println(storeValues.get(x));
  80. }
  81. }
  82.  
  83. }
Java Syntax (Toggle Plain Text)
  1.  
  2. public class Main {
  3.  
  4. /**
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9. String fileName="C:/Documents and Settings/tntadmin/workspace2/a.csv";
  10. CSVFileReader x=new CSVFileReader(fileName);
  11. x.ReadFile();
  12. x.displayArrayList();
  13.  
  14. }
  15.  
  16. }
Last edited by darkknightgaury; Jul 21st, 2010 at 6:50 pm. Reason: Mistake
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darkknightgaury is offline Offline
4 posts
since Oct 2006
Jul 21st, 2010
0

Thank for your input.

Feel free to comment and tell me how to make this better.
Last edited by darkknightgaury; Jul 21st, 2010 at 6:52 pm. Reason: Want peer review
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darkknightgaury is offline Offline
4 posts
since Oct 2006
Jul 24th, 2010
0

Reading individual lines

Is there a way to print only a single line. Let's say that the a.csv files has 50 lines, how can I chose only line 20?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
heckyj is offline Offline
1 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Executing a batch file from java code.
Next Thread in Java Forum Timeline: How to add a child node to specific parent node in the JTree?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC