Problem with splitting data from a .csv file (ArrayIndexOutOfBoundsException)
in
Programming Questions
•
1 year ago
Hi guys,
maybe trivial but I can't get out of it. I'm importing a list of tweets from a .csv file (using this code:
http://cwwang.com/2008/02/23/csv-import-for-processing).
I'm splitting each line to have the tweet content, and other information (date and time, which come in this form: "Tue, 08 May 2012 09:58:03 +0000"). So to use hours and minutes separately I need to split them again. Which I do in an array called tweet_info. If I access tweet_info[0] works fine, if I access any other value (e.g. tweet_info[1]) it gives me an ArrayIndexOutOfBoundsException (found something similar here, but I couldn't sort it out:
https://forum.processing.org/topic/arrayindexoutofboundsexception).
If anybody can help I'll be very grateful,
thanks.
// CODE
String lines[] = loadStrings("bigsociety_tweets_2012_05_15.csv");
String [][] csv;
int csvWidth=0;
String [] tweet_info;
//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];
}
}
tweet_info = new String [lines.length];
//test
for (int i=1; i < lines.length; i++) {
if (csv[i][4] != null) {
tweet_info = split(csv[i][4], " ");
println(tweet_info);
}
}
// EOC
Printing tweet_info seems to give a correct output, that I shoul be able to access with tweet_info[n]:
[0] ""
[1] "05"
[2] "May"
[3] "2012"
[4] "17:46:35"
[5] "+0000""
1