java中的CTRL+F逻辑

我想在文本文件中搜索特定单词并将该单词存储在数组列表中。我已经这样做了,但它会给出文本文件中是否存在类似单词的输出。我想将该文本存储在数组列表中


double count = 0, countBuffer = 0, countLine = 0;

String lineNumber = "";

String filePath = "D:\\PDFTOEXCEL\\Extractionfrompdf.txt";

BufferedReader br;

String inputSearch = "Facture";

String line = "";


try {

    br = new BufferedReader(new FileReader(filePath));

    try {

        while ((line = br.readLine()) != null) {

            countLine++;

            //System.out.println(line);

            String[] words = line.split(" ");


            for (String word : words) {

                if (word.equals(inputSearch)) {

                    count++;

                    countBuffer++;

                }

            }


            if (countBuffer > 0) {

                countBuffer = 0;

                lineNumber += countLine + ",";

            }


        }

        br.close();

    } catch (IOException e) {


        e.printStackTrace();

    }

} catch (FileNotFoundException e) {


    e.printStackTrace();

}


ABOUTYOU
浏览 102回答 2
2回答

拉风的咖菲猫

double count = 0, countBuffer = 0, countLine = 0;String lineNumber = "";String filePath = "D:\\PDFTOEXCEL\\Extractionfrompdf.txt";BufferedReader br;String inputSearch = "Facture";String line = "";List<String> searchedWords = new ArrayList<>();try {&nbsp; &nbsp; br = new BufferedReader(new FileReader(filePath));&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countLine++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] words = line.split(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String word : words) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (word.equals(inputSearch)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countBuffer++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!searchedWords.contains(word)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; searchedWords.add(word);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (countBuffer > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countBuffer = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineNumber += countLine + ",";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; br.close();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }} catch (FileNotFoundException e) {&nbsp; &nbsp; e.printStackTrace();}System.out.println("Words that you have searched and found:");for(String word : searchedWords){&nbsp; &nbsp; System.out.println(word);}您将拥有一个searchedWords跟踪搜索单词的数组列表,您会注意到 if 语句不允许在数组列表中重复,因此如果您想允许重复,只需删除if(!searchedWords.contains(word))并写入searchedWords.add(word);.

梵蒂冈之花

您可以使用其他声明定义一个 String ArrayList:ArrayList<String> matches = new ArrayList<String>();然后,当您找到匹配的单词时,使用以下命令将其添加到 ArrayList:matches.add(word);编辑:double count = 0, countBuffer = 0, countLine = 0;String lineNumber = "";String filePath = "D:\\PDFTOEXCEL\\Extractionfrompdf.txt";BufferedReader br;String inputSearch = "Facture";String line = "";ArrayList<String> matches = new ArrayList<String>();try {&nbsp; &nbsp; br = new BufferedReader(new FileReader(filePath));&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countLine++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] words = line.split(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String word : words) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (word.equals(inputSearch)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countBuffer++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matches.add(word);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (countBuffer > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countBuffer = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineNumber += countLine + ",";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; br.close();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }} catch (FileNotFoundException e) {&nbsp; &nbsp; e.printStackTrace();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java