strings coming out as null in array
in
Programming Questions
•
22 days ago
I have a CSV file with a very basic setup. It's a 4 column, 3 row setup. Like this:
A, 1, 2, 3
B, 4, 5, 6
C 7, 8, 9
I want to simply expand it to 6 columns, then write it out as a CSV again. I don't care what the values are at this point for the expanded cells, I'm just curious why everything is coming out as null
Goes without saying that the saved file is also null null null null...
A, 1, 2, 3
B, 4, 5, 6
C 7, 8, 9
I want to simply expand it to 6 columns, then write it out as a CSV again. I don't care what the values are at this point for the expanded cells, I'm just curious why everything is coming out as null
- // the following adds functionality to che-wei wang's CSV import sketch
// lines.length is the number of rows, wheras csvWidth is the column count
String lines[] = loadStrings("db.csv");
String [][] csv;
int csvWidth=0;
//calculate max width of csv file
for (int i=0; i < lines.length; i++) {
String [] chars=split(lines[i], ',');
if (chars.length>csvWidth) {
csvWidth=chars.length + 2; // here we expand by 2 columns
}
}
//create csv array based on # of rows and columns in csv file
csv = new String [lines.length][csvWidth];
//parse values into 2d array
for (int i=0; i < lines.length; i++) {
String [] temp = new String [lines.length];
temp= split(lines[i], ',');
for (int j=0; j < temp.length; j++) {
csv[i][j]=temp[j];
}
}
csv[2][4] = "test";
// now save
// collapse to a single dimension array
String[] linesOut = new String[csvWidth * lines.length];
for (int i = 0; i < lines.length; i++) {
for (int z = 0; z < csvWidth; z++) {
linesOut[i] = csv[i][z];
println(linesOut[i]);
}
}
println(linesOut);
saveStrings("out.csv", linesOut);
//test
println(lines.length);
Goes without saying that the saved file is also null null null null...
1