I have some code in processing which reads in a CSV file and dumps it into an array. I then need to use this data in various methods to draw some exciting graphics.
However in the code below When I try to run the glyph method I get an exception that the program cant find anything called DSA. Can anyone point me in the right direction? Someone told me I should put “public” before defining the string but that just caused another error (unexpected token).
void setup() {
cp5 = new ControlP5(this);
cp5.addButton("Overview")
.setValue(0)
.setPosition(840, 10)
.setSize(100, 19);
cp5.addButton("Quadrant")
.setValue(0)
.setPosition(840, 30)
.setSize(100, 19);
cp5.addButton("Location Map")
.setValue(0)
.setPosition(840, 50)
.setSize(100, 19);
String [][] DSA = readFile("DSA.csv");
String [][] NC = readFile("NC.csv");
String [][] IW = readFile("IW.csv");
size(950, 600);
smooth();
//noStroke();
//Use system font 'Arial' as the header font with 12 point type
h1 = createFont("Arial", 12, false);
//Use system font 'Arial' as the label font with 9 point type
l1 = createFont("Arial", 9, false);
}
String [][] readFile(String fileName) {
//for importing csv files into a 2d array
//by che-wei wang
String lines[] = loadStrings(fileName);
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;
}
}
//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];
}
}
return csv;
}
void Gluph() {
println(DSA[1][3])
}
This is a scoping problem. Read about variable scope.
One solution is you could declare the variables in the global space. Right now they are declared in your setup() function, and once setup() exits, the variables are no longer available to other functions since they were declared within the setup() scope. If you declare them before setup(), on the very first line of your program, you’ll have access to them in the global scope.
If you need to still readFile() within setup, then just declare the variables in the global scope and assign values in setup(). Since the variables are declared in the global scope, the changes in the setup() scope will still be reflected no matter where you access them from.