Home > Mobile >  Java .split method returning empty array
Java .split method returning empty array

Time:11-09

I am trying to count all the words in each sentence in an array of multiple sentences automatically from a file in eclipse.

When I'm splitting the paragraph into sentences the java .split method is returning an empty array

Here is the code that is causing me trouble

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

here is my sentence 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;
    }
}

And finally, here is my text file (it is only one line of text)

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.

Here is the output

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

CodePudding user response:

Replace paragraph.split(".") by paragraph.split("\\.")

  • Related