-1

I'm currently trying to take a large text and split the text sentence by sentence. I have the code working to split the text into separate sentences but its of course also including spaces. What would I need to add to my code to make it omit spaces in the sentence array?

    String [] sentence
    ArrayList <String> sentenceList = new ArrayList <String> ();
    try {
        Scanner sentenceScanner = new Scanner (new File("data/" + fileName));
        while (sentenceScanner.hasNextLine()) {
            sentenceList.add (sentenceScanner.nextLine());
        }
        sentenceScanner.close();
    } catch (FileNotFoundException e) {
        System.out.println ("File Not Found");
    }

    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 Answers1

0

It's better to filter while you are reading the input data:

while (sentenceScanner.hasNextLine()) {
    String line = sentenceScanner.nextLine().trim();
    if (!line.isEmpty()) {
        sentenceList.add (line);
    }
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42