не могу посчитать работает рекламный персонаж с pdfbox в Java - PullRequest
0 голосов
/ 23 ноября 2018
class ReadPDF {


    public void Read() throws IOException {

        int amountOfWords = 0;
        int amountOfChars = 0;
        String sourceCode ="";

        try {
            PDDocument doc = PDDocument.load(new File("C:\\Users\\ccw\\Desktop\\articles\\RECYCLING-BEHAVIOUR-AMONG-MALAYSIAN-TERTIARY-STUDENTS.pdf"));
            String text = new PDFTextStripper().getText(doc);

            sourceCode = sourceCode.replace ("-", "").replace (".", "");

            while(doc!=null){
                String[] words = sourceCode.split(" ");
                amountOfWords = amountOfWords + words.length;
                for (String word : words) {
                    amountOfChars = amountOfChars + word.length();
                }
            }

            System.out.println("Amount of Chars is " + amountOfChars);
            System.out.println("Amount of Words is " + (amountOfWords + 1));
            System.out.println("Average Word Length is "+ (amountOfChars/amountOfWords));


        }catch (IOException e) {
            System.out.println(e);
        }

    }

}

Я пытаюсь посчитать все слова и символы в файле PDF с помощью pdfbox.Но теперь я получаю сообщение об ошибке, sourceCode не инициализируется

1 Ответ

0 голосов
/ 23 ноября 2018

Замените эту строку sourceCode = sourceCode.replace ("-", "").replace (".", ""); на sourceCode = text.replace ("-", "").replace (".", "");. И удалите цикл while

...