Im trying to write a program that accepts two input parameters:
1. The string to search for
2. The directory to search in
For each file in the specified directory the program counts the number of times the specified string occurs in the file.
I'm assuming that all the files in the directpry are .text.
I seem to have hit a brick wall, maybe I've been looking at it too long but my java is not that strong anyway. The traverse method works on its own, the search method works on its own but I cant get them to work together or to give me the number of times the string occurs in each file.
Any help would be greatly appreciated, I'm exhausted!
Yes sebastian sorry.
Do you see on line 43, print count. Well i want it to print the number of times the String, that the user has entered, occurs. It gives me a number alright but its not correct.
I think the problem is in the search method.
The program should accept a directory from the user. Go through this directory if it exists, get all the files and then search all of these files for a String the user enters (String gets inputed in the search method) Then print the number of times the string occured in each file.
Sebastian Janisch wrote:Are you positive your regular expression that you pass as the search needle is correct ?
No I'm not positive at all but I think so, I had it work before on its own and it seemed to be alright.
Do you get any exceptions/errors? Check if system is able to locate the files and directory.
And is lines 59 and 60 necessary?
No They dont need to be there anymore.
I dont get any exceptions for some of the paths I'm using and it accepts string(directory) as input, returns true, asks for string input, returns a number (not the number of times the word was counted) and continues repeating that, enter string etc, until it ends. None of the numbers printed out correspond with the correct results. I'm not sure what the program is counting for me but it doesnt seem to be what I want
For another path I try, I get a fileNotFount error (the system cannot find the file specified), with the names of the files in the particular directory, which is odd if it cant find them !
The overall program when its finished should actually start a thread for each file in the specified directory, that counts the number of times the specified string occured.
I thought that maybe I could get it working without threads for a start and then try implementing them. Is this a bad idea?
Do you want to search a String or a pattern???
I assume that you are searching for String.
At line 39, you've tried to increment count variable everytime it finds the match.(didn't you??)
Now there's a catch. You have checked m.find() inside while loop. So even if a match is found,
count variable will be incremented only once per line. So thats the reason why are you not getting
the correct "count".
replace the code inside the while loop with following...
I've not looked at a single line of your code, but i'd start verifying a few things...
I'd first make sure the program is finding the right number of files. It probably is, if you're just grabbing all, but it never hurts to verify.
next, see if it's finding all the strings in a single file. you could either a) only put ONE file in the dir at a time (while testing) and see if it works, then rotate which file is there, or b) when it's done with a given file, have it print out the file name and the number of occurrences it found for that file.
as it stands, you don't know if the problem is a) it is not finding all the files, b) it is not finding all the values in a given file, c) it is not totaling things correctly.
When you're testing against one file, make sure you have some files with 0 hits, some with 1, and some with more than one. You may have a fence-post problem where you're dropping the last one or counting the same one twice somehow.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Why is the word "abbreviation" so long? And this ad is so short?