簡體   English   中英

從 java 中的文本文件中搜索多個字符串

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

我正在嘗試從一個 txt 文件中搜索用戶給出的多個單詞(我使用數組將它們存儲在其中),然后如果該單詞在文件中出現過一次,它將被顯示,如果不是,則不會。 同樣對於單詞本身,如果它是重復的,它將搜索一次。

現在的問題是,當我只搜索一個它有效時,但是對於多個單詞,它會不斷重復,即使這個詞存在,它也不存在。

我想知道我應該把 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();
       }
    }


您編寫的代碼容易出錯,請記住在使用 while 循環時始終應該有適當的中斷條件。

試試下面的代碼:

 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);
  }
}

我已經嘗試通過對值進行硬編碼,您可以從文件和控制台中獲取單詞和段落。

用於從文本中提取單詞的“正確”class 是java.text.BreakIterator

您可以嘗試以下操作(在大文件的情況下逐行讀取)

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>...]");
    }
}

樣本運行:

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