2015-11-19 44 views
0

我想讀取文本文件的前3個句子並將其顯示爲TextView。我的意圖是在完全停止後分割每個句子並將每個句子發送到數組列表。然後顯示存儲在數組列表中的前三個句子。這是我必須通過文本循環並獲得前3行的代碼。閱讀文本文件的前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. 

我該如何解決它?

回答

2

您正在將完整列表追加到每個句子的文本中。

改變這一行:

text.append(sentence); 

這樣:

text.append(sentence.get(sentence.size()-1)); 

希望這有助於。

+0

感謝它的工作。最後在代碼中應該有一個額外的「)」。 – user5455438

+0

對,編輯,很高興它幫助。 – Nanoc

1

刪除+「。」 from sentence.add刪除多餘的添加期間(您的代碼在文本中找到一個,並在此處添加第二個)。括號不會被這個刪除。

+0

爲什麼要解決這個問題? – Nanoc

+0

謝謝,它停止了額外的句號,但主要問題仍然存在 – user5455438

+0

感謝Nanoc。編輯。 – SteveO

相關問題