2009-12-09 89 views
0

我有一個小型的java程序,用於搜索特定字符串文件夾中所有* .txt文件的內容。我的問題搜索完全匹配正在返回子字符串。

例子:

  1. 輸入字符串發現:
  2. 輸入字符串發現:6570
  3. 輸入字符串發現:6570
    發現2 kyle.txt時間(s) !
    在kylezz.txt找到2時間!
    按任意鍵繼續。 。 。

問題:

它正在尋找6570,但是我攝像結果與該字符串這樣的值:

 
11111116570111111 
657011111 
111116570 
6570 

問:我要搜索只有一個確切的字符串,例如:「6570」。我怎麼才能讓它返回只有 6570的確切值?我不想在開始或結束時添加任何額外的字符,只有確切的值。

這裏是我的代碼:

import java.io.*; 
import java.util.*; 

public class FileScanner { 

    public static void main(String args[]) { 

     System.out.print("Enter string to find: "); 
     Scanner sc = new Scanner(System.in); 
     find(sc.nextLine()); 

    } 

    public static void find(String delim) { 
     File dir = new File("files"); 
     if (dir.exists()) { 
      String read; 
      try { 
       File files[] = dir.listFiles(); 
       for (int i = 0; i < files.length; i++) { 
        File loaded = files[i]; 
        if (loaded.getName().endsWith(".txt")) { 
         BufferedReader in = new BufferedReader(new FileReader(
           loaded)); 
         StringBuffer load = new StringBuffer(); 
         while ((read = in.readLine()) != null) { 
          load.append(read + "\n"); 
         } 
         String delimiter[] = new String(load).split(delim); 
         if (delimiter.length > 1) { 
          System.out.println("Found " 
            + (delimiter.length - 1) + " time(s) in " 
            + loaded.getName() + "!"); 
         } 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } else { 
      System.out.println("error: dir wasn't found!"); 
     } 
    } 
} 

謝謝你們我真的希望你能幫助我與我的編程問題。我一直試圖修復它大約一個月,現在我正在尋求幫助。

+0

格式化你的源代碼或者它是不可能讀的 – rachvela 2009-12-09 19:27:30

回答

0

會這樣的工作?

public static void find(String delim) { 
    File dir = new File("/tmp/files"); 
    Pattern strPattern = Pattern.compile(delim); 
    if (dir.exists()) { 
     try { 
      for(File curFile : dir.listFiles()){ 
       if(!curFile.getName().endsWith(".txt")){continue;} 
       BufferedReader in = new BufferedReader(new FileReader(
         curFile)); 
       int foundCount = 0; 
       String read = null; 
       while ((read = in.readLine()) != null) { 
       if(strPattern.matcher(read).matches()){ 
        foundCount ++; 
       } 
       } 
       System.out.println("Found "+ delim +" "+ foundCount + " time(s) in "+curFile); 

      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } else { 
     System.out.println("error: dir wasn't found!"); 
    } 
} 
+0

這是如何避免匹配子字符串的問題? – 2009-12-09 21:31:45

+0

模式匹配確切。我對他的輸入文件運行它,它只匹配一行。 – Kylar 2009-12-11 18:17:25

0

取而代之的是:

String delimiter[] = new String(load).split(delim); 
             if(delimiter.length > 1) { 
               System.out.println("Found " + (delimiter.length - 1) + " time(s) in " + loaded.getName() + "!"); 
             } 

您可以使用此,我想你會得到你想要的東西:

String delimiter[] = new String(load).split(" "); 
int counter = 0; 
for(int i = 0; i < delimeter.length; i++){ 
    if(delimeter[i].equals(delim)) counter++; 
} 
System.out.println("Found " + counter + " time(s) in " + loaded.getName() + "!"); 
1

一個非常簡單的解決辦法是對的每一側添加空格你正在尋找的字符串。

1

您不想將您的搜索字詞當作分隔符。如果我已經正確理解你的問題,你想匹配確切的字?用「單詞」表示空格分隔的字符串。沒有進入正則表達式,你可以做最簡單的事情是:

int matchCount = 0; 

while((read = in.readLine()) != null) { 

    String[] words = read.split("\\s+"); // split the line on white space 

    for (String word : words) {    // loop thru the resulting word array 
     if (word.matches(searchTerm)) matchCount++;  // if exact match increment count 
    }         
} 

如果您搜索的是「7683」,這將匹配單詞「7683」,而不是「67683」或「7683h」 - 這是完全符合。