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:
- Enter string to find:
- Enter string to find: 6570
- 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.