[英]split file sentences into words
can anyone help me please? 有人可以帮我吗?
I'm trying to split the sentences[]
into words[]
. 我正在尝试将
sentences[]
分解为words[]
。 But it shows Syntax error on token "j", delete this token
... 但是它
Syntax error on token "j", delete this token
显示Syntax error on token "j", delete this token
...
My code is: 我的代码是:
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(" ");
}
}
}
What can I do? 我能做什么?
String word[j]=sentences[i].split(" ");
^^^^^^^^
this is not a valid String
or String array
declaration. 这不是有效的
String
或String array
声明。
String.split()
returns an array. String.split()
返回一个数组。 So you have to change 所以你必须改变
String word[j] = sentences[i].split(" ");
to this 对此
String[] word = sentences[i].split(" ");
Instead of for loop with j variable use: 代替使用j变量的for循环,请使用:
String[] words = sentences[i].split(" ");
For multidimensional array: 对于多维数组:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.