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,773
  • 5
  • 28
  • 61
owlprogrammer
  • 177
  • 1
  • 2
  • 8

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
  • 374,785
  • 51
  • 663
  • 734
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,218
  • 9
  • 34
  • 57