为了账号安全,请及时绑定邮箱和手机立即绑定

java中的CTRL+F逻辑

java中的CTRL+F逻辑

ABOUTYOU 2022-05-21 19:53:23
我想在文本文件中搜索特定单词并将该单词存储在数组列表中。我已经这样做了,但它会给出文本文件中是否存在类似单词的输出。我想将该文本存储在数组列表中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();}
查看完整描述

2 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超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 {

    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(!searchedWords.contains(word)){

                        searchedWords.add(word);

                    }


                }

            }

            if (countBuffer > 0) {

                countBuffer = 0;

                lineNumber += countLine + ",";

            }

        }

        br.close();

    } catch (IOException e) {

        e.printStackTrace();

    }

} catch (FileNotFoundException e) {

    e.printStackTrace();

}


System.out.println("Words that you have searched and found:");

for(String word : searchedWords){

    System.out.println(word);

}

您将拥有一个searchedWords跟踪搜索单词的数组列表,您会注意到 if 语句不允许在数组列表中重复,因此如果您想允许重复,只需删除if(!searchedWords.contains(word))并写入searchedWords.add(word);.


查看完整回答
反对 回复 2022-05-21
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

您可以使用其他声明定义一个 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 {

    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++;

                    matches.add(word);

                }

            }


            if (countBuffer > 0) {

                countBuffer = 0;

                lineNumber += countLine + ",";

            }


        }

        br.close();

    } catch (IOException e) {


        e.printStackTrace();

    }

} catch (FileNotFoundException e) {


    e.printStackTrace();

}


查看完整回答
反对 回复 2022-05-21
  • 2 回答
  • 0 关注
  • 101 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信