简体   繁体   English

从 java 中的文本文件中搜索多个字符串

[英]search for multiple strings from a text file in java

I'm trying to search of multiple words given from a user ( i used array to store them in ) from one txt file, and then if that word presented once in the file it will be displayed and if it's not it won't.我正在尝试从一个 txt 文件中搜索用户给出的多个单词(我使用数组将它们存储在其中),然后如果该单词在文件中出现过一次,它将被显示,如果不是,则不会。 also for the words itself, if it's duplicated it will search it once.同样对于单词本身,如果它是重复的,它将搜索一次。

the problem now when i search for only one it worked, but with multiple words it keeps repeated that the word isn't present even if it's there.现在的问题是,当我只搜索一个它有效时,但是对于多个单词,它会不断重复,即使这个词存在,它也不存在。

i would like to know where should i put the for loop and what's the possible changes.我想知道我应该把 for 循环放在哪里以及可能的变化是什么。

package search;
import java.io.*;
import java.util.Scanner;

public class Read {
    public static void main(String[] args) throws IOException 
       {
        Scanner sc = new Scanner(System.in);
        
          String[] words=null;  
          FileReader fr = new FileReader("java.txt");  
          BufferedReader br = new BufferedReader(fr);
          
          String s;     
          
          System.out.println("Enter the number of words:");
          Integer n = sc.nextInt();
          
          String wordsArray[] = new String[n];
          System.out.println("Enter words:");
          for(int i=0; i<n; i++)  
          {
              wordsArray[i]=sc.next();  
          }

           
         for (int i = 0; i <n; i++) {
             int count=0;   //Intialize the word to zero
              while((s=br.readLine())!=null)   //Reading Content from the file
          {
            {
             words=s.split(" ");  //Split the word using space
             
              for (String word : words) 
              {
                     if (word.equals(wordsArray[i]))   //Search for the given word
                     {
                       count++;    //If Present increase the count by one
                     }
              }
              
          if(count == 1)
          {
             System.out.println(wordsArray[i] + " is unique in file ");
          }
          else if (count == 0)
          {
             System.out.println("The given word is not present in the file");
          }
          else
          {
             System.out.println("The given word is present in the file more than 1 time");
          }
          }
            }
             }
             fr.close();
       }
    }


The code which you wrote is error prone and remember always there should be proper break condition when you use while loop.您编写的代码容易出错,请记住在使用 while 循环时始终应该有适当的中断条件。

Try the following code:试试下面的代码:

 public class Read {
  
  public static void main(String[] args)
  {

    // Declaring the String
    String paragraph = "These words can be searched";
    // Declaring a HashMap of <String, Integer>
    Map<String, Integer> hashMap = new HashMap<>();

    // Splitting the words of string
    // and storing them in the array.
    String[] words = new String[]{"These", "can", "searched"};

    for (String word : words) {

      // Asking whether the HashMap contains the
      // key or not. Will return null if not.
      Integer integer = hashMap.get(word);

      if (integer == null)
        // Storing the word as key and its
        // occurrence as value in the HashMap.
        hashMap.put(word, 1);

      else {
        // Incrementing the value if the word
        // is already present in the HashMap.
        hashMap.put(word, integer + 1);
      }
    }
    System.out.println(hashMap);
  }
}

I've tried by hard coding the values, you can take words and paragraph from the file and console.我已经尝试通过对值进行硬编码,您可以从文件和控制台中获取单词和段落。

The 'proper' class to use for extracting words from text is java.text.BreakIterator用于从文本中提取单词的“正确”class 是java.text.BreakIterator

You can try the following (reading line-wise in case of large files)您可以尝试以下操作(在大文件的情况下逐行读取)

import java.text.BreakIterator;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class WordFinder {

    public static void main(String[] args) {
        try {
            if (args.length < 2) {
                WordFinder.usage();
                System.exit(1);
            }
            ArrayList<String> argv = new ArrayList<>(Arrays.asList(args));
            String path = argv.remove(0);
            List<String> found = WordFinder.findWords(Files.lines(Paths.get(path)), argv);
            System.out.printf("Found the following word(s) in file at %s%n", path);
            System.out.println(found);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static List<String> findWords(Stream<String> lines, ArrayList<String> searchWords) {
        List<String> result = new ArrayList<>();
        BreakIterator boundary = BreakIterator.getWordInstance();
        lines.forEach(line -> {
            boundary.setText(line);

            int start = boundary.first();
            for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
                String candidate = line.substring(start, end);
                if (searchWords.contains(candidate)) {
                    result.add(candidate);
                    searchWords.remove(candidate);
                }
            }
        });
        return result;
    }

    private static void usage() {
        System.err.println("Usage: java WordFinder <Path to input file> <Word 1> [<Word 2> <Word 3>...]");
    }
}

Sample run:样本运行:

goose@t410:/tmp$ echo 'the quick brown fox jumps over the lazy dog' >quick.txt
goose@t410:/tmp$ java WordFinder quick.txt dog goose the did quick over
Found the following word(s) in file at quick.txt
[the, quick, over, dog]
goose@t410:/tmp$ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM