VJay February 2016

im getting following exception when doing following code: java.lang.StringIndexOutOfBoundsException

//for loop for traversing rows
for (int i = 1 ; i < roorws.getCount() ; i++)
        {   // for loop for traversing columns
            for (int j = 1 ;j<tcols ; j++)
             {  //if column is 5
                if(j==5)

                 //printing values of cell 5 

                 System.out.print(cells.get(i,j).getValue().toString().substring(0, 26).length()+ "\t");
                 String cell1=cells.get(i,j).getValue().toString().substring(0, 26);

                 cell=cells.get("F2");
                 cell.putValue(cell1);
             }
        System.out.println("");
        }

Answers


Zbynek Vyskovsky - kvr000 February 2016

Apparently the length of the string is not (at least) 26 characters. Change:

System.out.print(cells.get(i,j).getValue().toString().substring(0, 26).length()+ "\t");

to:

System.out.print(cells.get(i,j).getValue().toString().length()+ "\t");

to print the length of the string.

You can use Apache Commons StringUtils substring() method to get sub string in a safe way (so it takes only at most n characters):

String cell1= StringUtils.substring(cells.get(i,j).getValue().toString(), 0, 26);

Post Status

Asked in February 2016
Viewed 2,621 times
Voted 14
Answered 1 times

Search




Leave an answer