Reading csv file into 2-dimentional Array Java -


i'm trying write method take multiline csv file , return contents of file arraylist. problem is, when print array lines, appear have contents of last line of file. i'm suspecting might filereader or bufferedreader don't know. anyhow, here's code:

import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception;   public int[][] readcsv(string pfilename) throws numberformatexception, ioexception {     int[][] arr = new int[19][19];        bufferedreader br = new bufferedreader(new filereader(pfilename));        string line = " ";     string [] temp;      while ((line = br.readline())!= null){          temp = line.split(","); //split spaces          for(int = 0; i<arr.length; i++) {             (int j = 0; j<arr.length; j++) {                     arr[i][j] = integer.parseint(temp[j]);             }         }      }      printarray(arr); }  public static void printarray (int[][] arr) {           (int =0; <arr.length; i++) {           (int j = 0; j < arr.length; j++) {             system.out.print(arr[i][j]);           }           system.out.println("");          }     }           } 

input

1,1,0,0,1,1,1,1,1,0,0,1,1,0,0,0,1,1,1
1,0,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1
1,0,0,1,0,0,1,1,1,1,1,1,1,1,1,0,1,1,0
1,1,0,1,0,1,1,0,1,0,1,0,0,1,1,1,0,1,1
1,1,0,1,1,1,0,1,0,1,0,0,1,1,1,0,1,0,0
1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1
1,1,1,0,0,0,1,1,1,0,1,1,1,1,1,0,1,0,1
0,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,1,1,1
1,1,1,1,1,0,0,1,1,1,1,0,1,0,0,1,1,0,1
1,1,1,0,1,1,1,0,1,0,1,1,1,1,0,1,1,1,1
0,0,1,1,1,1,1,0,0,1,0,1,1,0,1,1,0,1,0
1,0,0,0,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0
1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1
1,0,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1
1,1,1,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,0
1,0,0,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1,0
1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1
0,1,0,0,1,1,0,1,0,1,1,1,0,1,1,0,1,1,1
1,1,0,0,1,1,1,1,1,1,0,0,1,1,0,1,0,1,0
0,1,1,1,0,0,1,1,1,1,1,1,0,1,0,1,1,1,0

print output

0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110
0111001111110101110

while ((line = br.readline())!= null){          temp = line.split(","); //split spaces          for(int = 0; i<arr.length; i++) {             (int j = 0; j<arr.length; j++) {                     arr[i][j] = integer.parseint(temp[j]);             }         }      } 

that because rewriting whole array every input line. last 1 "survives".

you want keep i index tied current line number (just i++ every iteration of outer loop, don't nest loop).


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -