使用一个文本文件搜索另一个文本文件

时间:2013-08-06 12:48:23

标签: java while-loop java.util.scanner

所以我一直试图让它工作一段时间。让我先说一下,我不是程序员。这是我最近接受的一种爱好。我一直试图让2个文本文件逐行搜索。一个人有一堆词(大约10个,每行一个),另一个有更多(接近500)每行一个。我希望我的程序能够说出较小文本文件中的每个单词出现在较大文本中的次数。到目前为止我所拥有的是:

   import java.util.Scanner;  
   import java.io.File;  
   import java.util.regex.Pattern;  

   public class StringSearch 
   {  

   public static void main (String args[]) throws java.io.IOException
       {  
   int tot = 0;  
   Scanner scan = null;  
   Scanner scan2 = null;
   String str = null;
   String str2 = null;


   File file = new File("C:\\sample2.txt");
   File file2 = new File("C:\\sample3.txt");
   scan = new Scanner(file); 
   scan2 = new Scanner(file2);
      while (scan.hasNextLine()) 
      {
        str = scan.nextLine();
        tot = 0;
            while (scan2.hasNextLine())
            {
                str2 = scan2.nextLine();
                    if(str.equals(str2)) 
                    {
                 tot++;
                     }
             }
   System.out.println("The String = " + str + " and it occurred " + tot + " times");
        }

   }
   }

不确定为什么这不起作用。它会读取第一个文本文件中的第一个单词并计算它在第二个单词中出现的次数,但它只是停止并且不会移动到第一个文件中的第二个单词。我希望这是有道理的。我认为第二个while循环出了问题,但我不知道是什么。

所以,任何帮助将不胜感激。我希望将此工作转移到未来更复杂的项目中。要从哪里开始吧?

干杯伙伴

4 个答案:

答案 0 :(得分:0)

您遇到的问题是您在扫描仪中使用扫描仪。您当前扫描程序嵌套的方式,它会导致一个扫描程序完全读取第一个单词的整个文本文件,但在第一个单词之后,它已经读取了整个文件,并且永远不会返回{{1 }}

达到所需要的更好方法是雷米亚贝尔所说的。您应该创建一个数组,其中包含小文件中的所有单词,每次在其他文件中执行单词时都会迭代这些单词。你还需要创建一些东西来跟踪每个单词被击中的次数,这样你就可以使用像hashmap这样的东西。

看起来有点像这样:

scan2.hasNextLine()

答案 1 :(得分:0)

创建缓冲读卡器并将文件读入<String, Integer>

的地图
String filename = args[0];
BufferedReader words = new BufferedReader(new FileReader(FILENAME));
Map<String, Integer>m = new HashMap<String, Integer>();
for(String word: words.readLine()){
    if(word!=null && word.trim().length()>0) {
        m.add(String, 0);
    }
}

然后读取单词列表并在每次找到时增加地图的值:

String filename = args[1];
BufferedReader listOfWords = new BufferedReader(new FileReader(FILENAME2));
for(String word: listOfWords.readLine()){
    if(word!=null && word.trim().length()>0) {
        if(m.get(word)!=null){
            m.add(word, m.get(word) + 1);
        }
    }
}    

然后打印结果:

for(String word: map.keys()){
     if(map.get(word)>0){
         System.out.println("The String = " + word + " occurred " + map.get(word) + " times");
     }
}

答案 2 :(得分:0)

使用嵌套循环的方法会扫描第二个文件中第一个文件中的每个单词。这将是非常低效的。我建议在HashMap

中加载第一个文件

不仅可以利用快速查找功能,还可以轻松更新发生次数。更不用说,你只会扫描第二个文件,并且会自动忽略第一个文件中的任何重复项(因为结果会相同)。

Map<String, Integer> wordCounts = new HashMap<String, Integer>();

Scanner scanner = new Scanner("one\nfive\nten");
while (scanner.hasNextLine()) {
    wordCounts.put(scanner.nextLine(), 0);
}
scanner.close();

scanner = new Scanner("one\n" + // 1 time
                      "two\nthree\nfour\n" +
                      "five\nfive\n" + // 2 times
                      "six\nseven\neight\nnine\n" +
                      "ten\nten\nten"); // 3 times

while (scanner.hasNextLine()) {
    String word = scanner.nextLine();
    Integer integer = wordCounts.get(word);
    if (integer != null) {
        wordCounts.put(word, ++integer);
    }
}
scanner.close();

for (String word : wordCounts.keySet()) {
    int count = wordCounts.get(word);
    if (count > 0) {
        System.out.println("'" + word + "' occurs " + count + " times.");
    }
}

输出

'ten' occurs 3 times.
'five' occurs 2 times.
'one' occurs 1 times.

答案 3 :(得分:0)

这只是一个简单的逻辑问题..

在System.out.println

下添加以下语句

scan2 = new Scanner(file2);

相关问题