0

I'm new to java programming and i have been working with this problem for the last 2 weeks.Somehow i managed to extract the paragraph from the doc file using getParagraphText() method.Now that I'm confused about finding a match with the given string and to copy all the paragraphs that contains the given string into another doc file.

sam alex
  • 13
  • 1
  • 8

1 Answers1

0

First, you need to change your WriteDocFile method to accept a list of paragraphs because you want to print all matched paragraph (and change your method name to writeDocFile , java use camel case for method name)

public static void writeDocFile(List<String> paras) {
    XWPFDocument document = null;
    try {
        document = new XWPFDocument();
        FileOutputStream out = new FileOutputStream(new File("matchedpara.docx"));
        for (String para : paras) {
            XWPFParagraph paragraph = document.createParagraph();
            paragraph.setSpacingAfter(1);
            paragraph.createRun().setText(para);
        }
        document.write(out);
        out.close();
        System.out.println("matchedpara.docx written successfully");
    } catch (Exception er) {
        er.printStackTrace();
    }
    finally {
        try {
            if (document != null) {
                document.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And in your main function, you already have paragraph list, so just use it to compare with the key, you don't have to read the file again (you can not use Scanner for reading doc file, though. It only work with text file)

// if you want to write empty string to doc file where there isn't any match
private static final List<String> NOT_ANY_MATCHED = new ArrayList<String>(){{ add(""); }}; 
if (paragraphs.length > 0) {
    List<String> matches = new ArrayList<>();
    for( String para : paragraphs ) {
        para = para.trim();
        if(para.contains(key)) { 
            matches.add(para);
        }
    }
    if (matches.size() > 0){
        writeDocFile(matches);
    }
    esle {
        writeDocFile(NOT_ANY_MATCHED);
    }
}
Hai Hoang
  • 1,207
  • 2
  • 18
  • 35
  • i got this error, cannot find symbol method close() – sam alex Apr 09 '18 at 03:31
  • Did you mean `document.close()` ? – Hai Hoang Apr 09 '18 at 03:55
  • Yes i was talking about that one – sam alex Apr 09 '18 at 04:09
  • Hmm, this is strange, the code run fine in my machine. [close](http://poi.apache.org/apidocs/org/apache/poi/POIXMLDocument.html#close--) is a method of `POIXMLDocument` class which `XWPFDocument` devired from. Could you provide full source code for me to check if i can reproduce the error? or At least, the full error message. – Hai Hoang Apr 09 '18 at 04:55
  • I forgot that `paragraphs` is an array, not a list, so use `paragraphs.length` instead of `paragraphs.size()` and check if it work or not. – Hai Hoang Apr 09 '18 at 13:44
  • The error message is `java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: org.apache.poi.xwpf.usermodel.XWPFDocument.close at Test.writeDocFile(Test.java:35) at Test.main(Test.java:69)` .shows similar error with ` if (paragraphs.size() > 0).` so i had to replace `paragraphs.size()` with `paragraphs.length `. Also is it possible to rewrite the _matchedpara.docx_ file with null characters if no match has been found ? – sam alex Apr 09 '18 at 13:45
  • see my edited answer for the for the write to file if there is not match part. Also, which tool you use to compile your code? – Hai Hoang Apr 09 '18 at 13:58
  • the new code works fine except the document.close() part . I use NetBeans IDE 8.2 to compile the code – sam alex Apr 09 '18 at 14:17
  • It is because netbean [alow you to run the code even there is some classes are not compilable](https://stackoverflow.com/questions/2333285/java-lang-runtimeexception-uncompilable-source-code-what-can-cause-this). My guess is that you miss some jars in your classpath. make sure you have these dependencies : `org.apache.poi:poi:3.17` `org.apache.poi:poi-scratchpad:3.17` `org.apache.poi:poi-ooxml:3.17` `org.apache.poi:poi-ooxml-schemas:3.17` `org.apache.poi:ooxml-schemas:1.3` – Hai Hoang Apr 09 '18 at 14:30
  • i was using apache poi 3.9 with apache poi 3.17 its working fine thank you very much – sam alex Apr 09 '18 at 14:43
  • It is better to to use eclipse or vscode for java development. Also if an answer resolve your problem, don't forget to mark it as "accepted". – Hai Hoang Apr 09 '18 at 15:05
  • I wish to create GUI for this program and my friends told me to use NetBeans that's why I'm using it. – sam alex Apr 09 '18 at 15:10