从文本文件中读取并拆分句子的Java操作

3

我有一段代码,可以从文本文件中读取每个句子并将其存储到数组中。以下是该代码:

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.

有没有什么办法可以让程序从每行开头一直显示到最后,以显示文件中的所有句子?谢谢!

3个回答

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]);
     }

  }

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循环内添加第二个for循环应该会有所帮助 :) !

0

导致所有问题的行是

SENTENCE = sentenceArray[r].split("(?<=[.!?])\\s*");

在每次迭代中,您都会用新值替换 SENTENCE 的先前值,因此会出现此问题,

将 for 循环合并肯定会解决问题,但如果您想在此练习之后使用 SENTENCE 数组的内容,则无济于事。

希望这可以帮助您!

祝你好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接