我有一个代码,它将从文本文件中读取并将每个句子存储到一个数组中。这是代码:
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.
任何人都知道如何让程序从行的开头到最后一行显示文件中的所有句子?谢谢!
答案 0 :(得分:4)
您必须将第二个循环放在第一个循环中,否则它只打印最后一个SENTENCE
值的结果:
for (int r=0;r<sentenceArray.length;r++)
{
SENTENCE = sentenceArray[r].split("(?<=[.!?])\\s*"); //split sentences and store in array
for (int j=0;j<SENTENCE.length;j++)
{
System.out.println("Sentence " + (j+1) + ": " + SENTENCE[j]);
}
}
答案 1 :(得分:4)
一种方法是:
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[sentenceList.size()]);
for (int r=0;r<sentenceArray.length;r++)
{
SENTENCE = sentenceArray[r].split("(?<=[.!?])\\s*");
for (int i=0;i<SENTENCE.length;i++)
{
System.out.println("Sentence " + (i+1) + ": " + SENTENCE[i]);
}
}
}
在第一个内部添加第二个for循环应该有帮助:)!
答案 2 :(得分:0)
导致所有问题的行是
SENTENCE = sentenceArray[r].split("(?<=[.!?])\\s*");
在每次迭代期间,您将使用新值替换先前的SENTENCE值,因此出现此问题,
使用for循环的俱乐部肯定会解决问题,但是如果你想在这个练习之后使用SENTENCE数组的内容那么它就不会有用了。希望这有帮助!
祝你好运!