| | |
Reading in a *.csv file and loading the data into an Array
![]() |
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
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.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
•
•
Join Date: Mar 2004
Posts: 762
Reputation:
Solved Threads: 38
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.
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)
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();
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
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)
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++; }
•
•
Join Date: Apr 2006
Posts: 5
Reputation:
Solved Threads: 2
•
•
•
•
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)
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();
@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.
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 ?"
"How to ask questions the smart way ?"
![]() |
Similar Threads
- Parsing a CSV File and inserting into a dictionary (Python)
- reading .csv file and getting the data out of it (Java)
- Reading CSV file into a ADO recordset (ASP.NET)
- Need Help Reading a csv file created from MSExcel (C)
Other Threads in the Java Forum
- Previous Thread: Storing image on a class
- Next Thread: how to change postfix to infix expression
| Thread Tools | Search this Thread |
-xlint add android applet application applications array automation bank bi binary blackberry block bluetooth class clear client code compile compiler component database development developmenthelp dice eclipse equation error event formatingtextintooltipjava fractal functiontesting game gameprogramming givemetehcodez graphics gui health html hyper idea image infinite int j2me j2seprojects java javac javame javaprojects jetbrains jni jpanel jtable julia learningresources lego linux mac main map method mobile myregfun netbeans notdisplaying number online openjavafx pearl problem program qt scanner screen scrollbar server set singleton sms sort spamblocker sql sqlserver storm string superclass swing system textfields thread threads time title tree tutorial-sample update variablebinding windows xor






