• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What's wrong with the following code? Please suggest

 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ashish Dutt
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic