如何仅使用 Scanner 读取文件并将每个句子存储在 arrayList 中?

我在做这件事时遇到了很多麻烦,由于这是一项任务,我无法发布我的整个代码。我已经能够成功地将每个单词存储到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. 感谢您的帮助!


萧十郎
浏览 244回答 2
2回答

蝴蝶不菲

你的逻辑有点不对。当您阅读next()then 时nextLine(),nextLine()将不包括所阅读的内容,next()因此您每次迭代都会跳过一个单词。试试这个:- 用useDelimiter();读到句号、感叹号或问号(句末)的方法读一行使用 a.作为分隔符的示例:Scanner in = new Scanner("Hello. This is a string. It has multiple senteces").useDelimiter("\\.");while(in.hasNext()) {&nbsp; System.out.println(in.next());}- 将句子添加到句子中 ArrayList()- 将句子拆分为单独的单词并将它们添加到单词中 ArrayList()

烙印99

下次给我们更多关于你的任务的细节,这样我们就不必猜测了:)无论如何,请使用以下结构来阅读每个句子:Scanner sc = new Scanner(inputFile).useDelimiter("\.");&nbsp;while (sc.hasNext()) {&nbsp; &nbsp; &nbsp;// we will hold your sentence in the s variable&nbsp; &nbsp; &nbsp;Sting s = sc.next();&nbsp; &nbsp; &nbsp;// here you add the string to your sentence array&nbsp; &nbsp; &nbsp;// or better be conscious about your memory usage&nbsp;&nbsp; &nbsp; &nbsp;// and do the processing right away.&nbsp; &nbsp; &nbsp;processSentence(s);}从您的代码片段看来,您似乎需要收集有关您的字数和最流行词的统计信息。HashMap<String, Integer>将是很好的结构。void processSentence(String s) {&nbsp; &nbsp; Scanner ws = new Scanner(s).useDelimiter(" ");&nbsp;&nbsp; &nbsp; while (ws.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; Sting w = ws.next();&nbsp; &nbsp; &nbsp; &nbsp; // I assume you have this.wordCounters initialized as HashMap<String,Integer>();&nbsp; &nbsp; &nbsp; &nbsp; Integer c = this.wordCounters.get(w);&nbsp; &nbsp; &nbsp; &nbsp; if (c==null) { c=0; }&nbsp; &nbsp; &nbsp; &nbsp; c = c+1;&nbsp; &nbsp; &nbsp; &nbsp; this.wordCounters.put(w, c);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java