我试图读取文本文件的前3句,并将其显示为TextView。我的意图是在句号结束后分割每个句子,并将每个句子发送到一个数组列表中。然后显示存储在数组列表中的前三句话。这是我必须循环遍历文本并获得前3行的代码。
reader = new BufferedReader(
new InputStreamReader(getAssets().open("inputNews.txt")));
String line;
List<String> sentence = new ArrayList<String>();
while ((line = reader.readLine()) != null) {
sentence.add(line.split("[\\.\\?\\!][\\r\\n\\t ]+")[0] + ".");
if(sentence.size() < 4){
text.append(sentence);
text.append('\n');
} 然后我用,
TextView output= (TextView) findViewById(R.id.summtext);
output.setText((CharSequence) text);但是,当我运行这个程序时,它会读取文件并打印出这样的文本,
[Sentence one..]
[Sentence one..Sentence two.]
[Sentence one..Sentence two. Sentence Three..]我不知道它为什么会产生双全站仪(.)还有[]。我的目的是想得到这样的结果,
Sentence one. Sentence two. Sentence three.我该怎么修呢?
发布于 2015-11-19 12:45:09
你在每个句子的文本后面加上完整的列表。
更改这一行:
text.append(sentence);对此:
text.append(sentence.get(sentence.size()-1));希望这能有所帮助。
发布于 2015-11-19 12:37:22
删除+“。从sentence.add中删除额外添加的句点(您的代码在文本中找到了一个,在这里添加了第二个)。这样就不会去掉括号了。
https://stackoverflow.com/questions/33804070
复制相似问题