Import Stings and Floats into an Array
in
Programming Questions
•
4 months ago
Hi,
I have a .csv file that has strings (locatoin names and urls) as well as floats and I want to import these into an array. I've seen this post, which creates a float array.
From the post above, I made two separate arrays, but I am wondering if there's a more efficient way of doing this.
Thanks,
Scott
I have a .csv file that has strings (locatoin names and urls) as well as floats and I want to import these into an array. I've seen this post, which creates a float array.
From the post above, I made two separate arrays, but I am wondering if there's a more efficient way of doing this.
Thanks,
Scott
- // load file
- String[] lines = loadStrings("data_2.csv");
- // get number of rows and columns
- int rows = lines.length;
- int cols = split(lines[0], ",").length;
- // create array
- float[][] floatArray = new float[rows][cols];
- // iterate over lines
- for (int i = 0; i < rows; i++) {
-
- // split current line
- String[] row = split(lines[i], ",");
-
- // iterate over values
- for (int j = 0; j< cols; j++) {
- // parse and store the value
- floatArray[i][j] = float(row[j]);
- }
- }
- String [][] stringArray = new String[rows][cols];
- // iterate over lines
- for (int i = 0; i < rows; i++) {
- // split current line
- String[] row = split(lines[i], ",");
-
- // iterate over values
- for (int j = 0; j< cols; j++) {
- // parse and store the value
- stringArray[i][j] = row[j];
- }
- }
- //test it out
- println(stringArray[0][2] + floatArray[2][2]);
1