【发布时间】:2021-10-24 07:19:09
【问题描述】:
您能帮我在这段代码中添加一个额外的检查,以帮助我找到每个段落的字数吗?
在此处输入代码
String path = "C:/CT_AQA - Copy/src/main/resources/file.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(new 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);
} 爪哇
【问题讨论】:
标签: java