Create a new sketch. Create a folder called 'data' in that sketch's directory. Copy the following and paste it into a file called 'text.txt' and save it in the new 'data' folder.
Code:
1,2,3,4,5,6,7
8,9,0,1,2,3,4
5,6,7,8,9,0,1
2,3,4,5,6,7,8
9,0,2,3,4,5,6
7,8,9,0,1,2,3
4,5,6,7,8,9,0
Put the following code into your new sketch:
Code:
LoadData test;
void setup(){
test = new LoadData("text.txt");
noLoop();
}
void draw(){
for(int i = 0; i < test.data.length; i++){
for(int j = 0; j < test.data[i].length; j++){
print(test.data[i][j] + " ");
}
println();
}
}
class LoadData{
String fileName;
int [][] data;
LoadData(String fileName){
String [] temp = loadStrings(fileName);
data = new int [7][7];
for(int i = 0; i < data.length; i++){
String [] tempLine = split(temp[i], ',');
for(int j = 0; j < data[i].length; j++){
data[i][j] = int(tempLine[j]);
}
}
}
}
You should have a working model that demonstrates what you were trying to achieve now. I've compacted what you were doing a lot because it seemed you could bunch all of your stuff into the class's constructor. I also set it up to demonstrate a method of iterating through a matrix of unknown size.
If there's anything that isn't too clear just say. I'll be happy to elaborate on my method. Hope this helps.