I’am having trouble to read data from a file and store on variables. I already tryed several diferents approachs but none of them were sucssesful.
The problem i’am having with this code is that i’am only reaciving data from the first line
float QNH, altitude;
float[] vals;
String[] str;
void setup(){
...
str = loadStrings("pressao.TXT");
String all = join(str, " ");
vals = float(split(all, " "));
}
void draw(){
int i = 0;
int j = 1;
altitude = vals[j];
QNH = vals[i];
...
(several functions)
...
i+=2;
j+=2;
}
There are two values in each line, i want to save the first value on the variable QNH, the second one on Altitude.
The data from the TXT looks like that:
1021.24 3096.03
1021.28 3097.36
1021.28 3097.36
1021.28 3097.36
1021.28 3097.36
1021.28 3097.36
1021.28 3097.36
1021.28 3097.36
1021.28 3097.36
1021.28 3097.36
1 Like
Would a for loop work?
for (int i = 0, j = 1; j < *the amount of lines*; i+=2, j+=2){
*the functions*
}
could be more specific? the only place i can see to put a - for- is inside the draw function, but draw itself already do that.
I think the code would have the same result
there are multiple lines in the text file EACH containing 2 values
so QNH and Altitude should be arrays.
OR you’re only reading the very first 2 values (in the 1st line)
OR you overwrite both values QNH and Altitude every time with the values from the next line
1 Like
yeah, i’ll try the variables as array, could work. thanks
I don’t think that’s correct
better for loop in draw
for(String s1 : str) {
String[] str2 = split(s1, " ");
// now
QNH = float(str2[0]);
Altitude= float(str2[1]);
}//for
2 Likes
i’ll try the variables as array, could work.
int i=0;
for(String s1 : str) {
String[] str2 = split(s1, " ");
// now
QNH[i] = float(str2[0]);
Altitude[i] = float(str2[1]);
i++;
}//for
2 Likes
I’am receiving nullPointerException error on this part, any clue?
(i’d declare the variables as array)
Do you have
float[] QNH=new float[250];
?
2 Likes
i didan’t. I don’t have ideia how, but it works haha. Thanks a lot!
1 Like
I don’t have idea how
This float[] QNH=new float[250];
just means: make a new array of the type float with 250 slots
There is a tutorial on arrays and one on data
https://www.processing.org/tutorials/data/
Also one about Strings
2 Likes