-1

I am trying to load a CSV file that has 80 rows and 5 columns, but i keep getting this error. I feel like i had tried every solution ive found. Here is my code.

float[][] data;
void setup() {
  String lines [] = loadStrings("data.csv"); 
 println(" There are " + lines.length + " lines ");

  data = new float[2][lines.length-1];
   
   String[] header = split(lines[0], '.');
  println(header[0] + "     " + header[1]);
  
 
  for (int i = 1 ; i < lines.length; i++)  {
   String[] dataStr = split(lines[i], '.');
   data[0][i-1] = float(dataStr[0]);
   data[1][i-1] = float(dataStr[1]);
  
  println(data[0][i-1] + " " + data[1][i-1]);
  }
}


 void draw (){
  
  
}

There are 81 lines ArrayIndexOutOfBoundsException: 1

println(header[0] + " " + header[1]); It says this is the line causing the problem

  • 3
    Why did you create as second dimension of the array (lines.lenght-1? – I love coding Aug 06 '20 at 19:12
  • could you please add the Error in the Log to your question. – julianpjp Aug 06 '20 at 19:12
  • Which line gives the error? The error dump should tell you. Start looking there. – rossum Aug 06 '20 at 19:13
  • println(header[0] + " " + header[1]); It says this is the line causing the problem And im following a tutorial but my code is not working even though it is the same – Wade Powell Aug 06 '20 at 19:18
  • Have you considered printing the length of the array before using it? Like `System.out.println("header.length = " + header.length);` – Scratte Aug 06 '20 at 20:18
  • 1
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Scratte Aug 06 '20 at 20:19
  • The only conclusion, based on the info that you gave us, is that the `split()` method is putting only either 0 or 1 item(s) in the header array.if header has only 1 item and the print statement references `header[1]` (the second index) the result will be `ArrayIndexOutOfBoundsException` – Nate T Aug 06 '20 at 21:37
  • Should the `.` in the params for the `split()` method be a `,`? That would explain the array with only 1 element... – Nate T Aug 06 '20 at 21:40
  • @WadePowell Any luck with my answer bellow ? – George Profenza Aug 08 '20 at 19:33

1 Answers1

0

It's easier to load/process CSV data in Processing using loadTable()

Notice this line in the example:

table = loadTable("data.csv", "header");

You can specify the table has a header which is pretty cool, because you could do something like:

float[] myColumn = table.getFloatColumn("myColumnName");

Another advantage is you can check the table was loaded/parsed successfully which would help verify / debug data. For example, if your table has string values which may contain comma(',') when it shouldn't as it's the separator

Speaking of separators, is you separator comma (which would be an CSV) or '.' ? :

String[] dataStr = split(lines[i], '.');

This is confusing because it may be also used for floating point values.

If you want to parse the data manually you can of course, but add more error checking as every step could have a problem. Here's an idea based on your code:

float[][] data;
void setup() {
  
  String lines [] = null;
  
  try{
    lines = loadStrings("data.csv"); 
    println(" There are " + lines.length + " lines ");
  }catch(Exception e){
    println("error loading data.csv");
    e.printStackTrace();
    exit();
  }
  
  data = new float[2][lines.length-1];

  String[] header = split(lines[0], ',');
  printArray(header);


  for (int i = 1; i < lines.length; i++) {
    String[] dataStr = split(lines[i], ',');
    
    if(dataStr == null){
      println("error parsing line",lines[i]);
      continue;
    }
    
    if(data.length < 2){
      println("line contains less than 2 values");
      printArray(dataStr);
      continue;
    }
    
    data[0][i-1] = float(dataStr[0]);
    data[1][i-1] = float(dataStr[1]);

    println(data[0][i-1] + " " + data[1][i-1]);
  }
}


void draw () {
}

It is assuming the CSV used comma(,) as a separator and I may be wrong: maybe the data is from a different locale where floating point numbers are separated by , instead of . and . dot is used to separate values ? Even so, you still need double check your CSV because that may still break parsing. For example:

println(float("3,14"));

prints NaN

where as

println(float("3.14"));

prints 3.14 as expected.

It might be easier to use find/replace all with a text editor on the CSV to ensure it uses , to separate values (between columns of a row) and . to separate the integer from the fractional part of a floating point number. If the CSV opens succefully and looks right in something like Excel / OpenOffice Calc / Google Sheets / etc. then it should(famous last words :)) easy to parse in Processing too.

George Profenza
  • 50,687
  • 19
  • 144
  • 218