java - 无法计数在java中使用pdfbox的广告字符
问题描述
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);
}
}
}
我正在尝试使用 pdfbox 计算 pdf 文件中的所有单词和字符。但现在我得到一个错误,sourceCode 没有初始化
解决方案
将此行替换sourceCode = sourceCode.replace ("-", "").replace (".", "");
为 . 并sourceCode = text.replace ("-", "").replace (".", "");
删除 while 循环
推荐阅读
- terraform - 如何针对单个 ECS 服务附加两个目标组
- jenkins - 在 GitHub 上重命名 jenkins
- node.js - Heroku - React 应用程序在 fetch() 请求中调用 localhost,而不是我的 Express 后端的 Heroku URL
- python - NaN 是否会干扰 pandas 中的列连接?
- php - 我的联系表格工作正常,直到我添加了附件选项。不再发送消息。知道如何验证附件字段吗?
- python-3.x - 有选择地更新数据框(python)的多级列名称,具有特定文本的列具有特定名称
- azure - Azure 自动化特权
- angularjs - 组件 templateUrl 不适用于规范
- installation - 在注册表中保存配置参数的位置以供将来在更新时参考
- javascript - 如何在 Tensorflow.js 中获得预测值