0

I have a 13 rows x 31 cell imported from a excel file and then store it inside arraylist so basically it is a 2d array like this

[1,2,3,4,5.................]
[a,b,c,d,e.................]

and then I convert it to String[] array like this

   String [] array = null;

      for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
            Row r = sheet.getRow(rowNum);

            ArrayList<String> dl = new ArrayList<String>();

            for (int i = 3; i < r.getLastCellNum() + 1; i = i + 3) {
                Cell cell = r.getCell(i);
                val = formatter.formatCellValue(cell);
                dl.add(val);

                array = dl.toArray(new String[dl.size()]);

            }
            // send array as parameter to another method inside the same class
            WriteNewFile(array);
            break;
        }

in another method to create a new excel file, I have to loop the array than contain data like array data above.

 for (int x= 0; x<= total - 1; x++) {
        Row row3 = sheet.getRow( x+ 2);
        Cell cell1 = row3.createCell(3);
        cell1.setCellValue(array[x]);
    }

when I run this code, I got error message like this:

java.lang.ArrayIndexOutOfBoundsException: 32

the aim of my code is I want to print the array in a single cell and loop the row until total-1.

the value of total is 396

UPDATED

 for (int x= 0; x< array.length; x++) {
        Row row3 = sheet.getRow( x+ 2);
        Cell cell1 = row3.createCell(3);
        cell1.setCellValue(array[x]);
    }

the looping for the java.lang.ArrayIndexOutOfBoundsException: 32 has been solved.

back to the main question, now I able to print out the first row only

[1,2,3,4,5.................] //of this row

how to continue for the next row? Do I better convert it to 1d array?

UPDATED

now after received the array as the parameter send by other method to writeNewFile method(writeNewFile method only task is to write the new excel file using apache poi of the array data) and I can write the data like this:

|test|
  1
  2
  3
  4
  5
  6

supposedly I want to loop the array to be continue loop for the next row like this

|test|
  1
  2
  3
  4
  5
  6
  a
  b
  b
  d
  e
sharky
  • 196
  • 1
  • 1
  • 14
  • when `x` is equal to `total -1` as per your loop, then `x + 2` is obviously going to exceed this value – Scary Wombat Oct 29 '18 at 00:07
  • @ScaryWombat please refer to my update – sharky Oct 29 '18 at 00:46
  • same problem is going to exist – Scary Wombat Oct 29 '18 at 00:47
  • Where do you think you have a 2D array? I only see `String [] array` which is not 2D. And you are calling `WriteNewFile(array);` for each row having `array` a 1D array of strings. We need to know what `WriteNewFile` is doing to determine what row it writes. Btw: `WriteNewFile` should not be the name of a method. Method names should start lowercase `writeNewFile`. Names starting uppercase are class names. – Axel Richter Oct 29 '18 at 04:41
  • @AxelRichter I am sorry. I am a totally beginner in programming world. I believe my arraylist data is 13row x 31cells. the array = dl.toArray(new String[dl.size()]); is I was trying to convert from arraylist to array because I need write every single data from the array which happens to have multirow array into a single cell while looping the row according to the data contain inside he array – sharky Oct 30 '18 at 08:45
  • @AxelRichter currently I can write the data only the first row of the array, but I cannot continue the next row of the array. thank you for telling me the name of the method btw – sharky Oct 30 '18 at 08:47
  • Your `String [] array` can only contain one row of data. And whoever wrote your code seems had have known this since `WriteNewFile(array)` is called for each single row. So we need to know what `WriteNewFile` is doing to be able to help. – Axel Richter Oct 30 '18 at 08:54
  • @AxelRichter see my latest update. Hope it can help you – sharky Oct 30 '18 at 09:09
  • We need the code of `WriteNewFile` method. – Axel Richter Oct 30 '18 at 09:11

0 Answers0