Java .split 方法返回空数组

Java .split method returning empty array

我正在尝试从 eclipse 中的文件中自动计算多个句子数组中每个句子中的所有单词。

当我将段落拆分为句子时,java .split 方法返回一个空数组

这是给我带来麻烦的代码

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

public class SentenceWordCounter {
    private static String paragraph;
    private static Sentence[] sentences;

    
    public SentenceWordCounter() {
        processFile("./src/Text");
        
    }
    
    public void sentenceByWordCount() {
        for (int i = 0; i < sentences.length; i++) {
            int sentenceLen = sentences[i].getWordCount();
            System.out.println("Sentence " + (i+1) + ": " + sentenceLen);
        }
    }
    
    private static void processFile(String filename) {        
        Scanner scan = null;
        try {
            scan = new Scanner( new File( filename ) );
            paragraph = scan.nextLine();
            String[] sentStrs = paragraph.split(".");
            System.out.println(paragraph);
            System.out.println(Arrays.toString(sentStrs));
            sentences = new Sentence[sentStrs.length];
            
            for (int i = 0; i < sentStrs.length; i++) {
                sentences[i] = new Sentence(sentStrs[i]);
            }
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        finally {
            if ( scan != null ) {
                scan.close();
            }
        }
    }
    
    public static void main (String args[]) {
        SentenceWordCounter strWordCount = new SentenceWordCounter();
        System.out.println(paragraph);
        System.out.println(Arrays.toString(sentences));
        strWordCount.sentenceByWordCount();
    }
}

这是我的句子class

import java.io.File;
import java.util.Scanner;

public class Sentence {
    
    private String[] words;
    private int wordCount;
    
    public Sentence(String str) {
        str = str.trim();
        
        words = str.split(" ");
        
        wordCount = words.length;
    }
    
    public int getWordCount() {
        return wordCount;
    }
}

最后,这是我的文本文件(只有一行文本)

Battles between good and evil and the allure of heroes have been a staple of storytelling for eons. The story of Beowulf, an epic tale, is about a man of immense strength and mental fortitude. He defeats great beasts such as the monster Grendel, Grendel’s mother, and even a dragon to save as many people as he can and to reap the rewards for his valor. Although he tragically dies in the end of the story, he dies as a hero. Beowulf’s story takes multiple forms. The epic was only an orally transmittable tale for the longest time, so it has been recorded in various mediums such as graphics or epic poems to preserve the story. Although the graphic novel and the poem have similar content, the effects the mediums have on the reader are vastly different.

这是输出

Battles between good and evil and the allure of heroes have been a staple of storytelling for eons. The story of Beowulf, an epic tale, is about a man of immense strength and mental fortitude. He defeats great beasts such as the monster Grendel, Grendel’s mother, and even a dragon to save as many people as he can and to reap the rewards for his valor. Although he tragically dies in the end of the story, he dies as a hero. Beowulf’s story takes multiple forms. The epic was only an orally transmittable tale for the longest time, so it has been recorded in various mediums such as graphics or epic poems to preserve the story. Although the graphic novel and the poem have similar content, the effects the mediums have on the reader are vastly different.
[]
Battles between good and evil and the allure of heroes have been a staple of storytelling for eons. The story of Beowulf, an epic tale, is about a man of immense strength and mental fortitude. He defeats great beasts such as the monster Grendel, Grendel’s mother, and even a dragon to save as many people as he can and to reap the rewards for his valor. Although he tragically dies in the end of the story, he dies as a hero. Beowulf’s story takes multiple forms. The epic was only an orally transmittable tale for the longest time, so it has been recorded in various mediums such as graphics or epic poems to preserve the story. Although the graphic novel and the poem have similar content, the effects the mediums have on the reader are vastly different.
[LSentence;@42a57993

paragraph.split(".")替换为paragraph.split("\.")