Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

search files in directory for string and return count

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

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!
Cheers
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you be more precise as to what exactly the problem is or where your program gets stuck ?
 
mike tully
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I hope that explains it a bit better.
Thanks
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you positive your regular expression that you pass as the search needle is correct ?
 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you get any exceptions/errors? Check if system is able to locate the files and directory.

And is lines 59 and 60 necessary?

Regards,
John Eipe
 
mike tully
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?

Thanks for your help guys
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
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...
 
lowercase baba
Posts: 12893
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Always look on the bright side of life. At least this ad is really tiny:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic