ArrayIndexOutOfBoundsException
Answered
- Need more info
- Answered
- Working on it
in
Programming Questions
•
3 years ago
I am having a problem with using two string arrays to get processing to parse data from a csv file with new data added every 5 seconds. I am able to get one array working to store a full line of data from the csv and then println new data every 5 seconds:
String[] consumption;
int index = 0;
void setup() {
consumption = loadStrings("powerdatalog.csv");
frameRate(0.20);
}
void draw() {
if (index < consumption.length) {
String[] allinfo = split(consumption[index], '\t');
if (allinfo.length == 1) {
float x = float(allinfo[0]);
}
println(allinfo[0]);
index = index + 1;
}
}
But when I try to use a second array to split those lines at the commas and only return the last value in a row, I get an ArrayIndexOutOfBoundsException:1 error message. Is there a better way to use arrays in order to 1. get only the newest string in the csv every 5 seconds and 2. split this string into individual values? I can do both of these things in separate sketches, but not at the same time in one sketch. Any help would be much appreciated! I'm wondering if perhaps I need to use functions instead of just having everything in the void draw? I'm new to processing and programming - I'm still getting used to the concept of arrays and the thing that I am not fully understanding here is the index part of all this, but I think it is necessary in order to get the array to hold only the latest csv value. Here is the sketch that is not working:
String[] consumption;
int index = 0;
void setup() {
consumption = loadStrings("powerdatalog.csv");
frameRate(0.20);
}
void draw() {
if (index < consumption.length) {
String[] allinfo = split(consumption[index], '\t');
if (allinfo.length == 1) {
float x = float(allinfo[0]);
}
println(allinfo[0]);
String[] watt = splitTokens(allinfo[index], ", ");
if (watt.length == 1) {
float y = float(watt[3]);
}
println(watt[3]);
index = index + 1;
}
}
String[] consumption;
int index = 0;
void setup() {
consumption = loadStrings("powerdatalog.csv");
frameRate(0.20);
}
void draw() {
if (index < consumption.length) {
String[] allinfo = split(consumption[index], '\t');
if (allinfo.length == 1) {
float x = float(allinfo[0]);
}
println(allinfo[0]);
index = index + 1;
}
}
But when I try to use a second array to split those lines at the commas and only return the last value in a row, I get an ArrayIndexOutOfBoundsException:1 error message. Is there a better way to use arrays in order to 1. get only the newest string in the csv every 5 seconds and 2. split this string into individual values? I can do both of these things in separate sketches, but not at the same time in one sketch. Any help would be much appreciated! I'm wondering if perhaps I need to use functions instead of just having everything in the void draw? I'm new to processing and programming - I'm still getting used to the concept of arrays and the thing that I am not fully understanding here is the index part of all this, but I think it is necessary in order to get the array to hold only the latest csv value. Here is the sketch that is not working:
String[] consumption;
int index = 0;
void setup() {
consumption = loadStrings("powerdatalog.csv");
frameRate(0.20);
}
void draw() {
if (index < consumption.length) {
String[] allinfo = split(consumption[index], '\t');
if (allinfo.length == 1) {
float x = float(allinfo[0]);
}
println(allinfo[0]);
String[] watt = splitTokens(allinfo[index], ", ");
if (watt.length == 1) {
float y = float(watt[3]);
}
println(watt[3]);
index = index + 1;
}
}
1