我在做这件事时遇到了很多麻烦,由于这是一项任务,我无法发布我的整个代码。我已经能够成功地将每个单词存储到ArrayList作业要求的类型中,但我确实需要将每个句子存储到 一个 中ArrayList,但是这样做有很多困难。
import java.util.*;
import java.io.*;
import java.lang.*;
public class WordLookUp {
private String[] mostWords;
private String line;
private List<String> original;
private List<String> mostOccur = new ArrayList<String>();
private List<Integer> count = new ArrayList<Integer>();
private String token;
private List<String> sentences = new ArrayList<String>();
private String sToken;
private Scanner reader2;
private String[] ss;
public WordLookUp(String file) throws Exception {
try (Scanner reader = new Scanner(new File(file));){
this.original = new ArrayList<String>();
this.sToken = null;
while (reader.hasNext()) { //reads file and stores it in string
this.token = reader.next();
this.original.add(this.token); //adds it to my arrayList
findMostOccurringWords(this.token);
this.sToken = reader.nextLine(); //how can I make this read and store sentences only
this.sentences.add(this.sToken);
}
}
}
}
如您所见,我已经使用过,reader.nextLine()但这当然只是在文件中存储行。我通过打印来测试它:
public void print() {
for (String s : this.sentences) {
System.out.println(s);
}
}
这证实了这一点。但是,我无法找到如何拆分ArrayList(我认为您不能)或如何简单地将每个句子放入句子的索引中ArrayList。我不能使用任何内置库,例如Collectionsor Array,我必须手动弄清楚如何将每个句子存储在ArrayList. 感谢您的帮助!
蝴蝶不菲
烙印99
相关分类