我正在嘗試從 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();
}
}
這是我的句子課
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
uj5u.com熱心網友回復:
替換paragraph.split(".")為paragraph.split("\\.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/352781.html
上一篇:java.lang.IndexOutOfBoundsException:嘗試從RecyclerView中洗掉URL損壞的專案
下一篇:JavaBase64編碼丟失資料
