Loading...
Logo
Processing Forum
I would like to insert numbers in txt file "Head.txt" and insert it to a A_matrix but failed with error:
ArrayIndexOutOfBoundsException: 313

my Head.txt will be like for eg.
   1     2    3
   4     5    6
......
311 312 313

means it takes a form of 313 x 3 matrix..
my code will be as below..
Copy code
  1. BufferedReader reader;
  2. String line;
  3. int rows=0;
  4. double[][] A_matrix = new double[313][3];
  5.  
  6. void setup() {
  7.   // Open the file from the createWriter() example
  8.   reader = createReader("Head.txt");   
  9. }
  10.  
  11. void draw() {
  12.   try {
  13.     line = reader.readLine();
  14.   } catch (IOException e) {
  15.     e.printStackTrace();
  16.     line = null;
  17.   }
  18.   while(line != null){
  19.     String[] nums = split(line, ' ');
  20.     for(int j=0; j<3; j++){
  21.       A_matrix[rows][j] = Double.parseDouble(nums[j]);
  22.     }
  23.     rows++;
  24.   }
  25. }
I know that this is not a good program but I couldnt think of any better..Please help me..
I'll send through email or upload on some hosting webpage if anyone need the file Head.txt

Replies(5)

Don't read the file in draw() 60 times per second, do it once in setup().

See the loadStrings method. Then you can use a for loop to fill the array.
owh..didnt know that there is such a nice function..Thanks amnon!!
Well, the code above is OK... if you want to show a progressive result. The file isn't read wholly on each frame, but only one line is read on each frame.
The problem is that draw() continue to be called after the end of the file...

Some other issues:
- You define the size of the array at 313, but apparently you need only 313 / 3 entries.
- If the data really looks like that, you can just generate it. But perhaps you didn't show the real values.
- The while at line 18 isn't useful, a simple if would have done the job...
- And, well, indeed, loadStrings() is here to ease your life... It would avoid the AIOOB exception, since you cannot read past the end of the file.
thanks for the advice PhiLho..

-oopss..all the file was 939 so..939/3 = 313..
-yeah those values are used to show how much numbers i had in the txt file
-'if' is better..note taken
-yeah..nice loadStrings()..
thanks guys..you are my teachers..
I post the new code here..Hope it could help other people with the same problem as me..If the code could be improven, please do tell..

Copy code
  1. String rows[] = loadStrings("Head.txt");
  2. String[] columns;
  3. columns = splitTokens(rows[1]);
  4. double[][] matrixA = new double[rows.length][columns.length];

  5. println("there are " + rows.length + " rows");
  6. println("there are " + columns.length + " columns");

  7. //insert the numbers from txt file into matrixA
  8. for (int i =0 ; i<rows.length; i++){
  9.   columns = splitTokens(rows[i]);
  10.   for(int j=0; j<columns.length; j++){
  11.     matrixA[i][j] = Double.parseDouble(columns[j]);
  12.   }
  13. }
  14.  
  15. //check what's inside matrixA
  16. for(int i=0; i<rows.length; i++){
  17.   for(int j=0; j<columns.length; j++){
  18.     print(matrixA[i][j] + "\t");
  19.   }
  20.   print("\n");
  21. }


I got other questions here..There are libraries(JAVA's) for matrices operation such as EJML, JAMA etc..
I would like to use those so its easier to calculate matrices..but the problem is I dont know how to use it
for eg..
read the txt file and directly insert the numbers to the matrix that is created by the classess in the library..
Could someone whose experienced or know about this help??any ideas are appreciated..