如何读取文件中介于文件中两个选定单词之间的单词数?

问题描述 投票:0回答:0

嘿,我需要帮助我正在尝试编写一个程序,让用户输入两个单词并打印出文件(words.txt)中有多少单词落在这些单词之间。

但是我很难找到一种方法来打印出用户两个所选单词之间的单词数(单词数)。非常感谢帮助

这是我目前所拥有的

    private static void Excerise04(String fname) throws FileNotFoundException {
        Scanner console = new Scanner(System.in);
        System.out.println("Enter First Word");
        int firstWord = console.nextInt();
        System.out.println("Enter Second Word");
        int secondWord = console.nextInt();
        Scanner filescanner = new Scanner(new File(fname));
        while (filescanner.hasNext()) {
            ArrayList<String> set = new ArrayList<>();
            String line = filescanner.nextLine();
            set.add(line);
            System.out.println(set);
        }
    }
}

这里是“words.txt”的示例(太长了,把整个东西都贴出来)

devertebrated
devest
devested
devesting
devests
devex
devexity
devi
deviability
deviable
deviance
deviances
deviancies
deviancy
deviant
deviant's
deviants
deviascope
deviate
deviated
deviately
deviates
deviating
deviation
deviational

我正在尝试编写一个程序,用户输入两个单词并打印出文件(words.txt)中有多少单词落在这些单词之间。

例外的结果是这样的:

"在**偏差**和devexity之间有13个词

java java.util.scanner user-input
© www.soinside.com 2019 - 2024. All rights reserved.