2013-08-06 107 views
0

所以我一直在试图让这个工作一段时间。让我先说这个,说我不是程序员。最近我接受了这更多的爱好。我一直试图让2个文本文件逐行搜索。即One有一串单词(大约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"); 
     } 

    } 
    } 

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

所以,任何帮助将不胜感激。我希望能够在未来发挥作用并转向更复杂的项目。要开始正确的地方?

干杯家伙

+0

如果将第一个文件的所有单词加载到数组中,则只需从第二个文件中读取数据,并将其与数组内容进行比较。单词的数量足够小,不会占用太多内存,而且您只处理一个文件。 – 2013-08-06 12:50:53

+0

注意,使用shell:'fgrep -f sample2.txt sample3.txt' –

回答

0

要跨运行的问题是,您使用的扫描仪中的扫描仪。您目前将扫描仪嵌套的方式会导致一台扫描仪完整读取第一个单词的整个文本文件,但在第一次扫描之后,它已经读取整个文件,并且永远不会为scan2.hasNextLine()返回true。

一个更好的方式来实现你想要的是雷米贝尔所说的。您应该创建一个数组,其中包含您的小文件中的所有单词,每次通过其他文件中的单词时都会迭代该单词。您还需要创建一些内容来跟踪每个单词被击中的次数,以便您可以使用类似hashmap的内容。

看起来会沿此线:

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); 
//Will contain all of your words to check against 
ArrayList<String> dictionary = new ArrayList<String>(); 
//Contains the number of times each word is hit 
HashMap<String,Integer> hits = new HashMap<String, Integer>(); 
while(scan.hasNextLine()) 
{ 
    str = scan.nextLine(); 
    dictionary.add(str); 
    hits.put(str, 0); 
} 
    while (scan2.hasNextLine()) 
     { 
      str2 = scan2.nextLine(); 
      for(String str: dictionary) 
      { 
       if(str.equals(str2)) 
       { 
        hits.put(str, hits.get(str) + 1); 
       } 
      } 
     } 
    for(String str: dictionary) 
    { 
     System.out.println("The String = " + str + " and it occurred " + hits.get(str) + " times"); 
    } 
} 
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"); 
    } 
} 
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. 
0

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

添加以下语句下面的System.out.println

SCAN2 =新的扫描仪(文件2);

+0

这是如何解决OP的问题? – UditS

相关问题