Processing not showing data values and not reading cities?

Viewed 53

Hello im having trouble with processing where its not outputting the data of my columns? It's reading the rows but inside each row it outputs it as a 0 except for "Number" even though there is data inside them?

Also i dont know how to seperate the cities as it definitly is not a int value?

My code:

int[][] data;

void setup() {

size(800, 600);

String[] lines = loadStrings("datasheet.csv");
println("there are " + lines.length + " lines");   

String[] header = split(lines[0], ','); 
println(header[0] + " " + header[1] + " " + header[2] + " " + header[3] + " " + header[4]);


data = new int[5][lines.length-1]; 

for (int i = 1 ; i < lines.length; i++) {
String[] dataStr = split(lines[i], ',');

data[0][i-1] = int (dataStr[0]);
data[1][i-1] = int (dataStr[1]);
data[2][i-1] = int (dataStr[2]);
data[3][i-1] = int (dataStr[3]);
data[4][i-1] = int (dataStr[4]);

println (data[0][i-1] + "  " + data[1][i-1] + "  " + data [2][i-1] + "  " + data [3][i-1] + "  " + 
data 
[4][i-1]);

}
}

This is what it is outputting

enter image description here

1 Answers

There are many issues in your code which you need to address:

A. Instead of printing the header in the following way:

println(header[0] + " " + header[1] + " " + header[2] + " " + header[3] + " " + header[4]);

you could do it in the following elegant way:

println(String.join(" ", header));

B. You need to declare data as new int[no-of-rows][no-of-columns] but you have done it the other way round. Moreover, instead of using a hardcoded value like 5, you should have used header.length.

C. Printing the data can also be done elegantly using String::join in the same way as done above for printing the header.

Given below is the code incorporating these points:

String[][] data;

void setup() {
    size(800, 600);
    String[] lines = loadStrings("datasheet.csv");
    println("there are " + lines.length + " lines");
    String[] header = split(lines[0], ','); 
    println(String.join(" ", header));
    data = new String[lines.length -1][header.length];
    for (int i = 1 ; i < lines.length; i++) {
        String[] dataStr = split(lines[i], ',');
        data[i-1] = dataStr;
        println(String.join(" ", dataStr));
    }
}

Feel free to comment in case of any doubt/issue.

Related