aspose file tools*
The moose likes Java in General and the fly likes java linked lists and loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java linked lists and loops" Watch "java linked lists and loops" New topic
Author

java linked lists and loops

david headstream
Greenhorn

Joined: Jan 18, 2012
Posts: 3
I am trying to:

1)read a word from a txt file
2)search for the word in my linked list (at beginning it will be empty)
3)if the word is found then increment count for that word
4)if the word is not found then insert it into the list

The goal is to output all words from the txt file and sort them depending on how many times a word appears. So far I can read the txt file into a linkedlist called ll and then print it out again. I would really appreciate some advice on how to set up my loops to get where I need to go with this. I know I need something like this in my while loop:



Thanks in advance!
Here is my code: I call this getTXT(); from my main method

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38792
    
  23
Welcome to the Ranch

I think you need to stop coding and start writing, on paper with a pencil. You will need a large eraser too!
You need to write down exactly how you intend to implement your algorithm. Also how you are going to do the counting. Are you going to create WordAndCount objects, or use a Map-based counter?

Once you have got that worked out, you will know how to code it.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:
You need to write down exactly how you intend to implement your algorithm. Also how you are going to do the counting


And those steps you write down should be in English or pseudocode, not in Java, and they should be very simple and precise.
david headstream
Greenhorn

Joined: Jan 18, 2012
Posts: 3
I am severly impressed with the response time on these message boards : )

You are correct I sat down and started coding till I got stuck.. I want to use word and count objects; not a mapper. Your advice is sound, however I have been sitting here with my pencil and big eraser for a while and I still can not think of how to generate these count objects to correspond with the words that are being read in. That is why I posted; a gentle nudge or swift kick in the propper direction would be greatly appreciated.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38792
    
  23
I have written those parts of your WordAndCount class which are obvious from its name. Now work out how to implement the incrementing method, and what (if anything) replaces my ...
Work out how to iterate the List and compare the next word with your current objects.

Since you are using a LinkedList rather than a Map, your program will execute with all the verve and élan of an arthritic snail (at best quadratic time).
david headstream
Greenhorn

Joined: Jan 18, 2012
Posts: 3
I have never seen @Overide before..

Oracle says:
"Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message."

I see where you are headed with this code snippet. Although I would rather not use something I have never seen before.. Its like learning to juggle with chainsaws instead of beanbags.

I guess that I would call WordAndCount insidemy while loop when I am reading the file in. Is that what you are proposing with this?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38792
    
  23
Yes. You can put those WordAndCount objects into your List and call them with the next word from the file. I have kept quiet about anything which isn’t obvious. So you will have to work that out yourself.

You use the @Override annotation to catch spelling errors. . . or overloading instead of overridingIf you look at the documentation for java.lang.Object, you find both those methods incorporate subtle mistakes which the @Override annotation will expose.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java linked lists and loops