我在执行此操作时遇到了很多麻烦,由于这是一项任务,因此我无法发布整个代码。我已经能够按照作业要求将每个单词成功存储到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
的索引中。我不能使用Collections
或Array
之类的内置库,必须手动找出如何将每个句子存储在ArrayList
中。感谢您的帮助!
答案 0 :(得分:1)
您的逻辑有些偏离。当您先阅读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()
答案 1 :(得分:0)
下次给我们提供有关您的作业的更多详细信息,因此我们不必猜测:)
无论如何,请使用以下结构读取每个句子:
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);
}
}