我有一段代码,可以从文本文件中读取每个句子并将其存储到数组中。以下是该代码:
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class HelloWorld{
static String[] SENTENCE;
public static void main(String []args) throws Exception{
Scanner sentence = new Scanner(new File("assets/input7.txt"));
ArrayList<String> sentenceList = new ArrayList<String>();
while (sentence.hasNextLine())
{
sentenceList.add(sentence.nextLine());
}
sentence.close();
String[] sentenceArray = sentenceList.toArray(new String[0]);
for (int r=0;r<sentenceArray.length;r++)
{
SENTENCE = sentenceArray[r].split("(?<=[.!?])\\s*"); //split sentences and store in array
}
for (int i=0;i<SENTENCE.length;i++)
{
System.out.println("Sentence " + (i+1) + ": " + SENTENCE[i]);
}
}
}
这是input7.txt文件中的内容。
Shocking images of a Taiwan apartment complex felled like a tree by an earthquake have highlighted what is needed to build a structure that can withstand seismic shocks.
Like Taiwan, Japan is quake-prone -- it suffers about a fifth of the world’s most powerful tremors. It has used a mix of ancient and modern technologies to make its buildings increasingly quake-proof.
Lessons have been consistently learnt and building standards subsequently raised in the wake of deadly disasters such as the 1995 Kobe earthquake, which killed 6,434 people.
When a massive magnitude earthquake struck off northeastern Japan on March 11, 2011, the shaking in Tokyo was violent. But buildings -- including the nearly complete 634-metre (2,080 feet) Tokyo Skytree tower and other skyscrapers -- survived intact.
然而,这段代码只会读取并显示文件中最后一行的句子:
Sentence 1: When a massive magnitude earthquake struck off northeastern Japan on March 11, 2011, the shaking in Tokyo was violent.
Sentence 2: But buildings -- including the nearly complete 634-metre (2,080 feet) Tokyo Skytree tower and other skyscrapers -- survived intact.
有没有什么办法可以让程序从每行开头一直显示到最后,以显示文件中的所有句子?谢谢!