tags:

views:

91

answers:

5

So I written up a small script to search in a folder in the current directory named /files/

All files are in *.txt file format and the tool will find any string inside the file. This does not search for file names, only the contents of the file:

Example of my problem:

  1. Enter string to find:
  2. Enter string to find: 6570
  3. Enter string to find: 6570
    Found 2 time(s) in kyle.txt!
    Found 2 time(s) in kylezz.txt!
    Press any key to continue . . .


The problem:

It is searching for 6570 however I pickup results for values like this with that string:

11111116570111111
657011111
111116570
6570


Question: How can I make it so, when I search for any value or number or letter, it only finds that string? If I try to search for 6570, how can I make it return only the exact value of 6570? I do not want any extra gaining numbers, or extra ending numbers. Only the exact value.

I was thinking, equalsIgnoreCase would fix it but I wasn't sure how to add it in there.

Here is my code so people can help me easier.

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

Thanks guys I really hope you can help me with my programming problem. I've been trying to fix it for about a month now and now I'm asking for help.

+1  A: 

Your best bet would be to read Mastering Regular Expressions. In the meantime, perhaps a tutorial on word boundaries would give you the right idea.

Jonathan Feinberg
Learning regular expressions might be a little much for this.
Jay
A: 

Instead of this:

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

you can use this and I think you will get what you want:

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() + "!");
rachvela
+1  A: 

A very simple solution would be to add spaces on each side of the string you're searching for.

Jay
+1  A: 

You do not want to treat your search term as a delimiter. If I have understood your question correctly, you want to match exact words? By "word" I mean space-delimited character strings. Without getting into regular expressions, the simplest thing you could do is:

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

If your search term was "7683" this would match the word "7683" but not "67683" or "7683h" - it is the exact match.

Richard
A: 

Would something like this work?

  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!");
    }
}
Kylar
how does this avoid the issue of matching substrings?
Richard
The pattern match is exact. I ran it against his input file and it only matched the one line.
Kylar