当前位置: 首页 > 知识库问答 >
问题:

我如何在代码Java中计算文件中每个段落的字数?

甘兴学
2023-03-14

你能帮我在这段代码中添加一个额外的检查吗?这个检查可以帮助我找出每一段的字数?

BufferedReader br=新的BufferedReader(新的InputStreamReader(新的FileInputStream(path)));

String line = " ";
int countWord = 0;
int sentenceCount = 0;
int characterCount = 0;
int paragraphCount = 1;
int countNotLetter = 0;
int letterCount = 0;
int wordInParagraph = 0;

while ((line = br.readLine()) != null) {
    if (line.equals("")) {
       paragraphCount++;
    } else {
        characterCount += line.length();
        String[] wordList = line.split("\\s+");
        countWord += wordList.length;
        String[] sentenceList = line.split("[!?.:]+");
        sentenceCount += sentenceList.length;
        String[] letterList = line.split("[^a-zA-Z]+");
        countNotLetter += letterList.length;
    }

    letterCount = characterCount - countNotLetter;
}
br.close();
System.out.println("The amount of words are " + countWord);
System.out.println("The amount of sentences are " + sentenceCount);
System.out.println("The amount of paragraphs are " + paragraphCount);
System.out.println("The amount of letters are " + letterCount);

}爪哇

共有1个答案

傅阳炎
2023-03-14

段落中的单词总数应该与所有行的单词总数wordcount相同。

如果必须计算每段的字数,那么wordsinParage应该是整数列表list wordsperParage ,可以这样计算:

int wordInParagraph = 0;
List<Integer> wordsPerParagraph = new ArrayList<>();

while ((line = br.readLine()) != null) {
    if (line.equals("")) {
       paragraphCount++;
       wordsPerParagraph.add(wordInParagraph);
       wordInParagraph = 0;
    } else {
        characterCount += line.length();
        String[] wordList = line.split("\\s+");
        countWord += wordList.length;
        wordInParagraph += wordList.length; // !!!

        String[] sentenceList = line.split("[!?.:]+");
        sentenceCount += sentenceList.length;
        String[] letterList = line.split("[^a-zA-Z]+");
        countNotLetter += letterList.length;
    }

    letterCount = characterCount - countNotLetter;
}
// in case the last paragraph does not have trailing empty line
if (wordInParagraph != 0) {
   wordsPerParagraph.add(wordInParagraph);
}
 类似资料:
  • 本文向大家介绍使用Java计算文本文件中的段落数,包括了使用Java计算文本文件中的段落数的使用技巧和注意事项,需要的朋友参考一下 我们可以通过读取字符串中的段落来读取文件中的段落,然后根据“ \ r \ n”模式进行分割。请参阅下面的示例- 示例 在类路径中请看以下文本文件。 test.txt 测试器 这将产生以下结果- 输出结果

  • 问题内容: 我可以列出所有目录 我试图使用以下命令列出每个目录的内容并计算每个目录中的文件数 但这是求和的总和 有没有一种方法可以计算每个目录中的文件数? 问题答案: 假设您已找到GNU,请让其查找目录,然后让bash进行其余操作:

  • 我有一个文本字段列表,每个字段都有相同的类名。这是html代码: 每个文本字段都有相同的类名,我试图通过使用Selenium中的sendKeys方法向每个文本字段发送键来自动化这些字段。我使用JavaScript(chai,mocha,node)来运行我的自动化。 这就是我试过的。我尝试通过className使用findElements,遍历className,然后填充每个文本字段: 但它失败,出

  • 我试图编写一个程序,计算文本中字母、单词和句子的数量。我可以假设一个字母是从a到z的任何小写字符或从a到z的任何大写字符,任何由空格分隔的字符序列都应算作一个单词,任何句点、感叹号或问号的出现都表示一个句子的结束。 到目前为止,我可以正确地计算字母和句子的数量,但是我错过了单词的数量: 例如是! 输出应为:3个字母1个单词1个句子 我得到的是:3个字母0个单词1个句子 更新:在printf函数前面

  • null null null 关于我的输出的说明: 在第一段-“ax@gmail.com”是4次,在其他电子邮件中出现的次数最多。 在第二段中--“az@gmail.com”是3次,在其他电子邮件中出现的次数最多。 null null null 但如何达到我的目标,输出:4,3,3 请帮帮我.TIA

  • 问题内容: 就像在口吃的情况下,如果文本为“ dean”且乘数为3,则由提供的乘数指定的次数,结果将为“ dddeeeaaannn”。 没有得到所需的结果。我究竟做错了什么? 问题答案: 您只需要为字符串中的每个字符添加n次字符串本身。您需要遍历字符串,并将每个字符追加n次。 另外,另一种解决方案是使用正则表达式。