将java文档中的每个句子存储在数组中?

将java文档中的每个句子存储在数组中?,java,arrays,words,sentence,Java,Arrays,Words,Sentence,我想将文档中的每个句子拆分,并将每个句子存储在不同的数组中。每个数组元素都是 句子中的单词。但我离这不远了 int count =0,len=0; String sentence[]; String words[][]; sentence = name.split("\\."); count = sentence.length; System.out.print("total sentence: " ); System.out.println(count); int h; words =

我想将文档中的每个句子拆分,并将每个句子存储在不同的数组中。每个数组元素都是 句子中的单词。但我离这不远了

int count =0,len=0;
String sentence[];
String words[][];
sentence = name.split("\\.");
count = sentence.length;

System.out.print("total sentence: " );
System.out.println(count);
int h;  
words = new String[count][]; 

for (h = 0; h < count; h++) {
     String tmp[] = sentence[h].split(" ");
     words[h] = tmp;
     len = len + words[h].length;
     System.out.println("total words: " );
     System.out.print(len); 

     temp = sentence[h].split(delimiter);  

     for(int i = 0; i < temp.length; i++) {
        System.out.print(len);
        System.out.println(temp[i]);
        len++;
     }  
}
int count=0,len=0;
字符串句子[];
字符串[][];
句子=名称。拆分(“\\”);
计数=句子长度;
系统输出打印(“总句子:”);
系统输出打印项次(计数);
int-h;
words=新字符串[计数][];
对于(h=0;h
我无法理解您的代码,但以下是如何通过三行代码实现您的既定目标:

String document; // read from somewhere

List<List<String>> words = new ArrayList<>();
for (String sentence : document.split("[.?!]\\s*"))
    words.add(Arrays.asList(sentence.split("[ ,;:]+")));
字符串文档;//从某处读
List words=new ArrayList();
用于(字符串语句:document.split(“[.?!]”\\s*”)
add(Arrays.asList(句子.split(“[,;:]+”));
如果要将
列表
转换为数组,请使用
List.asArray()
,但我不建议这样做。列表比数组更容易处理。首先,它们会自动扩展(这是上面的代码如此密集的原因之一)


附录:(大多数)字符不需要在字符类内转义

您的输入字符串似乎存储在
main
中。 我不明白内部
for
循环应该做什么:它重复打印
len
,但不更新它

String sentences[];
String words[][];

// End punctuation marks are ['.', '?', '!']
sentences = name.split("[\\.\\?\\!]"); 

System.out.println("num of sentences: " + sentences.length);

// Allocate stogage for (sentences.length) new arrays of strings
words = new String[sentences.length][];

// For each sentence
for (int h = 0; h < sentences.length; h++) {
  // Remove spaces from beginning and end of sentence (to avoid 0-length words)
  // split by any white space character sequence (caution if using Unicode!)
  words[h] = sentences[h].trim().split("\\s+"); 

  // Print out length of sentence.
  System.out.println("words (in sentence " + (h+1) + "): " + words[h].length);
}
String语句[];
字符串[][];
//结尾标点符号为[','?','!']
句子=name.split(“[\\.\?\\!]”);
System.out.println(“句子数:+句子长度”);
//为(句子.长度)新字符串数组分配Stogge
单词=新字符串[句子.长度][];
//每句话
for(int h=0;h<句长;h++){
//删除句首和句尾的空格(以避免出现长度为0的单词)
//按任意空格字符序列分割(如果使用Unicode,请小心!)
单词[h]=句子[h].trim().split(\\s+);
//打印出句子的长度。
System.out.println(“单词(在句子“+(h+1)+”)中:“+单词[h]。长度);
}

这段代码没问题。我不知道如何在数组中存储单词。我确实把每一个单词从句子中分离出来了。但是我如何将它们存储在数组中呢?当您使用
split
方法时,它会为您提供一个数组,而无需创建一个数组。哦!但如何查看或访问阵列?我需要使用数组进行进一步计算。如果您执行类似于
words[]=someString.split(“”)的操作,则数组
words
包含围绕空格拆分的所有元素。现在您可以访问那些元素,如
单词[0]
单词[1]
,等等。还有什么不起作用的吗?实际长度只是为了看看代码是否起作用!我需要的是输入——我吃米饭。他是个男孩。输出like-array[1]={i,eat,rice}。数组[2]={he,is,a,boy}显示此错误-无法将列表解析为类型。无法将列表解析为类型。无法将ArrayList解析为类型。令牌“@user3503711”上出现语法错误。您需要
导入java.util.*;
。如果您使用的是java 6,请改用
新建ArrayList()