1

谁能帮帮我?

我正在尝试将其拆分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(" ");
        }
    }
}   

我能做些什么?

4

4 回答 4

1
String  word[j]=sentences[i].split(" ");
          ^^^^^^^^

这不是有效的StringString array声明。

于 2013-02-08T08:06:44.150 回答
0

String.split()返回一个数组。所以你必须改变

String word[j] = sentences[i].split(" ");

对此

String[] word = sentences[i].split(" ");
于 2013-02-08T08:07:07.957 回答
0

而不是使用 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
于 2013-02-08T08:08:01.700 回答
0

分裂看起来像String[]split(String regex)分裂

所以改变你String word[j] = sentences[i].split(" ");

String  word[] = sentences[i].split(" ");
于 2013-02-08T08:12:29.170 回答