Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes What's wrong with the following code? Please suggest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's wrong with the following code? Please suggest

Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Dear friends,

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?

Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

In the method checkWordInFile, i forgot to mention the following line

The purpose of PrintWriter here is that if the word is found in the ArrayList than it should be written in a file called "temp.txt"
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

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
rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 131

I think the problem lies in your while loop here.



the above will iterate the list, untill there is an element.

And if you don't do

the loop will simply keep running.

If you are using here,

I don't think you will need the while loop, unless you want to check every element for the "word-to-find".
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 318
    
    2
Hi Ashish,

Your question is what is wrong with your code.

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
badly.

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
designing stage!

Greetings,
Piet
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Thank you all for your valuable inputs.
I strongly suspect the culprit is the while loop.. I am still figuring out what is the issue there.
I will let you know once i find what is the loophole
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

let's suppose for a minute I have a bucket that can potentially hold some marbles. What is wrong with this algorithm for examining each marble in there:


How long will it take to empty the jar? What step is missing here?

The same thing is happening in your code. you ask if there is another token, but do you do the next logical thing to reduce the tokens?
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Dear friends,

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:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's wrong with the following code? Please suggest
 
Similar Threads
Help with assignment and JFileChooser
Word count problem.
Scan a text file.
Question on searching ArrayLists
How to read file line by line and word by word?