0

I am trying to read contents from list of word documents in a directory and getting specific value which is "Summary" from all the files. Here is my code below. When it tries to get the 2nd file's value its throwing Array Index out of bound exception

Can someone help me out to get rid of this issue

public static void main(String[] args) throws IOException {

    File folder = new File("C:\\Kiruba\\Test Folder\\Admin\\");
    File[] listfiles = folder.listFiles();
    for(int i = 0; i < listfiles.length; i++) {
        if(listfiles[i].isFile()) {
            FileInputStream fis = new FileInputStream(listfiles[i]);
            XWPFDocument docx = new XWPFDocument(fis);
            List<XWPFParagraph> paragraphlist = docx.getParagraphs();

            for(XWPFParagraph paragraph : paragraphlist) {
                String summary = paragraph.getText().split("Summary: ")[1];
                System.out.println(summary);
            }
        }
        else
            System.out.println("There is no files in the directory");
    }    
}
name not found
  • 602
  • 4
  • 12
Alfred
  • 13
  • 5

1 Answers1

0

Exception log would help in identifying the line which raises the exception. It seems that this line "paragraph.getText().split("Summary: ")[1]" would throw mentioned exception if there is no matching text i.e. "Summary: ".

You may want to open the 2nd file and verify that this text is present there.