2

I am reading a text file into an arraylist and getting them by line but I want to split each line and put in a two dimensional array however String [][] array=lines.split(","); gives me an error.

File file=new File("text/file1.txt");
ArrayList<String> lines= (ArrayList<String>) FileUtils.readLines(file);
String [][] array=lines.split(",");
singhakash
  • 7,891
  • 6
  • 31
  • 65
owlprogrammer
  • 187
  • 1
  • 2
  • 9

2 Answers2

4

You must split each element of the List separately, since split operates on a String and returns a 1-dimensional String array :

File file=new File("text/file1.txt");
ArrayList<String> lines= (ArrayList<String>) FileUtils.readLines(file);
String [][] array=new String[lines.size()][];
for (int i=0;i<lines.size();i++)
    array[i]=lines.get(i).split(",");
Eran
  • 387,369
  • 54
  • 702
  • 768
0

split() returns [] not [][]. try this :

File file=new File("text/file1.txt");
List<String> lines= (ArrayList<String>) FileUtils.readLines(file);
String [][] array= new String[lines.size()][];
int index = 0;
for (String line : lines) {
    array[index] = line.split(",");
    index++;
}
ikos23
  • 4,879
  • 10
  • 41
  • 60