gpt4 book ai didi

java - 我如何将多行作为一个段落来阅读

转载 作者:行者123 更新时间:2023-12-01 23:08:50 25 4
gpt4 key购买 nike

我想将行序列计算为段落,即它应该将行之间没有空格的行计算为 1 行,例如(段落)。到目前为止,我的代码对每一行进行计数,但不知道行的序列应计为 1。请提供有关如何解决此问题的任何帮助吗?

     Scanner file = new Scanner(new FileInputStream("/../printout.txt"));

int count = 0;

while (file.hasNextLine()){
file.nextLine();
count++;
}
System.out.println(count);
file.close();

最佳答案

您可以设置一个小型状态机,假设从空行到非空行表示您处于新段落中:

boolean lastWasText = false;
int paragraphCount = 0;
while (file.hasNextLine()) {
boolean thisIsText = file.nextLine().length() > 0;
if (!lastWasText && thisIsText) paragraphCount++;
count++;
lastWasText = thisIsText;
}

如果您不希望所有空格的行都被计数,请在 nextLine() 之后和 length() 之前添加 trim() >.

关于java - 我如何将多行作为一个段落来阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22335838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com