• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text Crawler

 
Mary Brennan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic