We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone. This is my first super-noob post here, so if I need to add more detail, please let me know.
I have the attached code which reads a log file, identifies the first character of each line and deletes the line if it is not a number (ASCII number).
The line which reads
ascii_dec = char_array[0];
is causing the debug error "ArrayIndexOutOfBoundsException: 0". If I comment that line out it prints "50" which corresponds to ASCII character 2, as it is the first character of the last line in the log file. I get the same results if I change the byte data types to int.
Why doesn't it like the code in question and how should I change this to work correctly? I know, I suck at programming, so I'm sorry my implementation and technique may not be conventional. Thanks for the help -SK
String[] lines;
byte[] char_array;
byte ascii_dec;
void setup(){
lines = loadStrings("log 11-23-2013 2-10pm.csv");
for(int i = 0; i < lines.length; i++){
//turn the line into individual char's
char_array = lines[i].getBytes();
//isolate the first char of the line
ascii_dec = char_array[0];
//test the first char to see if there is a number stored in the first entry of the line.
//If not an ascii number, then delete the line from original array
if(ascii_dec != 48 && ascii_dec != 49 && ascii_dec != 50 && ascii_dec != 51 && ascii_dec != 52 && ascii_dec != 53 && ascii_dec != 54 && ascii_dec != 55 && ascii_dec != 56 && ascii_dec != 57){
lines[i] = "";
}
}
//saveStrings("log2.csv", lines); //implement this later
//this is to test and make sure there is something in that element of char_array[]
println(char_array[0]);
}
void draw(){}
Answers
I wouldn't recommend this line:
because you change the value of lines.length, so for-loop can not work properly.
I would instead recommend to copy the value into a new array
Thanks, I will copy each line that begins with a number into a new array to save. Any idea about the original problem?
this test works
Nice, I like your approach. I think the problem lays in how I import the file to lines[]. I just tried your modification with the code:
lines = loadStrings("log 11-23-2013 2-10pm.csv");
and got the old problem. Perhaps there is a line with nothing in the csv file and that would make char_array[0] not exist?If this is the case, how would I test for a line containing nothing, like: "" ?
Also, I think the operators in the if statement are reversed.
Anybody have a way to deal with empty strings? This seems to be what's causing the original error, I just need a way to check for them and skip over them. Ideas?
Thanks, Chrisir, for your help to figure this out so far.
Maybe I'll check the length of the char_array
(untested)
one thing
if (ascii_dec < 48 || ascii_dec > 57) {
requires you to know what 48 and 57 are. it's not obvious. whereas
if (ascii_dec < '0' || ascii_dec > '9') {
helps make it clear that they are digits.
but perhaps even better is Character.isDigit()
http://www.tutorialspoint.com/java/character_isdigit.htm