如何仅使用扫描仪读取文件并将每个句子存储在数组列表中?



我在做这件事时遇到了很多麻烦,由于这是一个作业,我无法发布我的整个代码。我已经能够成功地将每个单词存储到作业所需的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.我不能使用任何内置的库,如CollectionsArray,我必须手动弄清楚如何将每个句子存储在ArrayList中。感谢您的帮助!

你的逻辑有点不对劲。当您先读next()然后读nextLine()时,nextLine()不会包括与next()一起阅读的内容,因此您每次迭代都会跳过一个单词。试试这个:

- 使用useDelimiter();方法阅读一行,直到句点、感叹号或问号(句子的结尾(

使用.作为分隔符的示例:

Scanner in = new Scanner("Hello. This is a string. It has multiple senteces").useDelimiter("\.");
while(in.hasNext()) {
System.out.println(in.next());
}

-将句子添加到句子ArrayList()

-将句子拆分为单独的单词并将它们添加到单词ArrayList()

下次请向我们提供有关您的作业的更多详细信息,这样我们就不必猜测:)

无论如何,请使用以下结构来阅读每个句子:

Scanner sc = new Scanner(inputFile).useDelimiter("."); 
while (sc.hasNext()) {
// we will hold your sentence in the s variable
Sting s = sc.next();
// here you add the string to your sentence array
// or better be conscious about your memory usage 
// and do the processing right away.
processSentence(s);
}

从您的代码片段中,您似乎需要收集有关字数和最热门单词的统计信息。HashMap<String, Integer>会是一个很好的结构。

void processSentence(String s) {
Scanner ws = new Scanner(s).useDelimiter(" "); 
while (ws.hasNext()) {
Sting w = ws.next();
// I assume you have this.wordCounters initialized as HashMap<String,Integer>();
Integer c = this.wordCounters.get(w);
if (c==null) { c=0; }
c = c+1;
this.wordCounters.put(w, c);
}
}

最新更新