Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java linked lists and loops

 
david headstream
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50267
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50267
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50267
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic