In my quest to craft quality code in Java, i seem to have been stumped once again. :-)
What i want to code is the following;
I want to read a file that contains lots of text and write the contents of it in an ArrayList. [ I written and tested this code, its given below]
The above code works fine. It reads the file alice.txt which actually is the open source text from Project Gutenberg. populateList method populates the ArrayList.
Step 2: I want to search the ArrayList for a specific word. So i created another method
As you can see above I'm using the contains method for ArrayList to search for that specific word.. But this checkWordInFile() does not provide any result? it just keeps on running and i have to forcefully stop the execution.
Do point out what is the problem in this method, I would appreciate if you give me a hint and not the solution because i want to work out the solution.
Actually i wanted to compare many words contained in the ArrayList but after reading many previous threads i realized that for multiple words to search in the ArrayList its best to use Set's or basically Collections. That will be my Step 3 but for now let me know what's wrong with the above code?
System.out.pintln() is your best friend. Stick tons of them in your method. Print when you enter. Print when you leave. Print out something each time you start a new loop iteration. print out what word you are looking for. print out what you are looking at.
I would also suggest you read the API on Scanner, and what EXACTLY hasNext does (and more importantly, what it DOESN'T).
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Fred and Rohit already pointed out some important issues. I would like to add one or two other issues.
They may not lead to runtime errors, but nevertheless.
You use 'list' as a static member of type ArrayList.
In your method populateList() you fill this list, but your return value is of type ArrayList<String>.
It surprises me that the compiler doesn't complain, but I think you are mixing a couple of things here
In your checkWordInFile method you create a new Scanner (with the problem already mentioned by F & R),
yet you search through your static list and never actually use this scanner. You shouldn't, because
you already performed this scanner action.
So, there's a lot of strange logic and mixing up of concepts here. My suggestion is: go back to the
Thanks Fred, Your analogy was spot on. I was able to figure out the problem in the while loop and did manage to fix it too.
So now the code that implements the while loop looks like as follows
what is did was i created a local integer variable called eof and gave it the value of ArrayList.size(). This acted as tokens and while i'm traversing the tokens of strings in the arraylist this variable would keep decreasing until the end.
So now that the while loop has been fixed, my second problem is that i want to compare the ArrayList for the presence of a certain pre-defined String. So i went ahead and used the contains() method of the ArrayList, but it does not seem to work.
If i remove the break keyword then it keeps printing Match found until end of ArrayList. By using the break keyword the code is not being justified because it simply breaks execution at the first instance of the search Word/ Word to find.
The while loop is doing its job by traversing through the entire ArrayList no problems there, but how do i fix the if statement so that it keeps searching for the desired word? Also, please do give me a hint as in how do i print the matched word.
Eagerly awaiting your suggestions.
Seems that i have found the solution, the logic is weird and thats why i need your help in understanding it.
I re-read the ArrayList and the Collections API and came across the difference between boolean hasNext() and next() methods for the List. If you see in my code i'm not using the Iterator therefore i presume this being the reason that i cannot use the above two methods.
So then i just tried the console object created from the Scanner Class and tried the following (using the console object was just a fluke.. :-( but it gave the right result))
And Eureka.. it worked. I have no clue why and how it worked and thats why i need your help to understand why the above trick or fallacy or whatever you choose to call it has worked.
I want to know that why using the following code has given me the desired result. Lets say in my alice.txt file there are four occurences of the word "Alice" than by using this if statement i get the desired result of 4 Matches found. The concern is how is the Scanner object able to do this and not the ArrayList object and if that's so, then what is the need for the ArrayList object then?
The complete method is as follows:
subject: What's wrong with the following code? Please suggest