[英]split file sentences into words
有人可以幫我嗎?
我正在嘗試將sentences[]
分解為words[]
。 但是它Syntax error on token "j", delete this token
顯示Syntax error on token "j", delete this token
...
我的代碼是:
try
{
int j;
String paragraph = sample.readFileString(f);
String[] sentences = paragraph.split("[\\.\\!\\?]");
for (int i=0;i<sentences.length;i++)
{
System.out.println(i);
System.out.println(sentences[i]);
for( j=0;j<=i;j++)
{
String word[j]=sentences[i].split(" ");
}
}
}
我能做什么?
String word[j]=sentences[i].split(" ");
^^^^^^^^
這不是有效的String
或String array
聲明。
String.split()
返回一個數組。 所以你必須改變
String word[j] = sentences[i].split(" ");
對此
String[] word = sentences[i].split(" ");
代替使用j變量的for循環,請使用:
String[] words = sentences[i].split(" ");
對於多維數組:
String paragraph = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras non varius nisi. In at erat est, sit amet consectetur est. ";
String[] sentences = paragraph.split("[\\.\\!\\?]");
String[][] words = new String[sentences.length][];
for (int i=0;i<sentences.length;i++) {
words[i] = sentences[i].trim().split(" ");
}
System.out.println(words[0][1]); //[sentence][word] - it would be second word of first sentence
split看起來像String[]split(String regex)
split
因此,請更改您的String word[j] = sentences[i].split(" ");
至
String word[] = sentences[i].split(" ");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.