File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Text Crawler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Text Crawler" Watch "Text Crawler" New topic

Text Crawler

Mary Brennan

Joined: Oct 29, 2012
Posts: 7
I'm kinda new to java and I'm currently trying to create a text file crawler. The program will request that file that user would like to start the crawler, a word the user wants to look for and the number of occurrences of the word the user wants to find. Each file will have one or more reference to another file that the program will find and add to a list for the program to search next. Once the program finds the number of occurrences requested the program should stop and output a message showing the total number of occurrences and the number of occurrences in each file. If the total number of occurrences doesn't equal the number requested the program should output the number found. I know how to do the output messages but I am having a slight problem getting the program to stop once the number of occurrences has been found. Can anyone help me with this? It would be greatly appreciated. I have placed the code I have written below:

Steve Luke

Joined: Jan 28, 2003
Posts: 4181

There are a number of things you could do:

1) You have a for loop and while loops. You can break out of them using a break; statement. i don't know if you would need more than one because it is hard to follow how deeply nested your code is (since you don't indent consistently).

2) Use a flag to indicate that the count is above the threshold. And add conditions to the while and for loops to stop when the flag is positive. For example, boolean thresholdReached = false; , then your for loop and while loops would be something like for (...; (Current Condition) && !thresholdReached; ...) and while ( (current condition) && !thresholdReached)

It might be easier if you organized your code with methods (at least). Maybe some Objects to keep track of things...

Mary Brennan

Joined: Oct 29, 2012
Posts: 7
Hi Steve, thanks for the reply. I did indent properly however, when I pasted my code in here the indention I did was removed in some places. Not entirely sure why. I've re-entered it below and added the indentation I think was removed last time. I was thinking the same about methods which I think I will implement. I would just like to figure out what is going wrong here first though. I tried a number of other ways including adding conditions to for and while loops but nothing I did seemed to fix the issue.

Suresh Sajja
Ranch Hand

Joined: May 12, 2009
Posts: 34

1. Your class name "searchFile" starts with lowercase letter. The first letter will be uppercase. Please read here for code conventions. CodeStyle
2. Your code is all cluttered in main method. If i have to write this, i would start with the following skeleton. If the code is separated as various methods, it has got more advantages: easy to read, easy to understand, less error-prone and many more..

Mary Brennan

Joined: Oct 29, 2012
Posts: 7
Thanks Suresh. We were never taught to name our classes with a capital, that is now something I will continue to do from now on. I know how to write the methods and I will rewrite my code using methods.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
There are conventions about how you code: you will find links in this recent post. It is important to conform to conventions, because other programmers will need to read your code.
I agree. Here's the link:
subject: Text Crawler
It's not a secret anymore!