有人可以帮我吗?
我正在尝试将sentences[]
拆分为words[]
。但它显示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(" ");
}
}
}
我该怎么办?
答案 0 :(得分:1)
String word[j]=sentences[i].split(" ");
^^^^^^^^
这不是有效的String
或String array
声明。
答案 1 :(得分:0)
String.split()
返回一个数组。所以你必须改变
String word[j] = sentences[i].split(" ");
到这个
String[] word = sentences[i].split(" ");
答案 2 :(得分:0)
而不是使用j变量的循环使用:
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
答案 3 :(得分:0)
拆分看起来像String[]split(String regex)
split
所以改变你的String word[j] = sentences[i].split(" ");
到
String word[] = sentences[i].split(" ");