• Post Reply Bookmark Topic Watch Topic
  • New Topic

Word Count using Collections  RSS feed

 
Sandeep Mukherji
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Objective: To take a txt file as input and produce an output file which states which word repeated in the file how many times.

Constraints: We can not use String class for that and have to use Collectons only.

My current approach: I took the characters of the file in the ArrayList and tried to create another ArrayList which adds the first arrayList if a blank space is encountered. I planed to take the second ArrayList serch for the occurences of the word by comparing with a HashSet having all the words in it.
Here is the full code:

But I failed Any sugessions please.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're parsing a file for words and you can't use String?
That's a ridiculous restriction.

My first thought would be to set up a tree. Ignore case: 26 possible branches at each level (in implementation there would be quite a few less) Parse character by character. Put a count at the leaves. Start back at the root whenever you hit punctuation or white space.

In the real world, use a HashSet<String, Integer>. (Edit: surely I did not mean Int, per Campbell)

[ May 06, 2008: Message edited by: Bill Shirley ]
[ May 06, 2008: Message edited by: Bill Shirley ]
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd create a Counter class with a public int member, which can be incremented. Saves creating a lot of Integer objects.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Bill Shirley, but surely you meant "Map<String, Integer>"?
 
Sandeep Mukherji
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Worked hard and arrived to the solution as follows. If the code have a scope of more optimization please contribute.


Thanks.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generify your List and Map and Set. Change the Iterator to a local variable; it may throw Exceptions if you have it as a field.

Why have you got so much code in the constructor?
Have you come across methods of the Character class which can tell whether a character is a letter, digit, etc? they may be easier for you to use.

If you are using f+"walkin.txt" can you add a File to a String? Will that code actually compile?

Will Object1.toString.compareTo(object2) compile? Because the compareTo method of String is parametrised to String it won't accept Object as a parameter.
 
Sandeep Mukherji
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
Generify your List and Map and Set. Change the Iterator to a local variable; it may throw Exceptions if you have it as a field.

Why have you got so much code in the constructor?
Have you come across methods of the Character class which can tell whether a character is a letter, digit, etc? they may be easier for you to use.


Thanks for the suggessions. It helped me to learn a lot.

If you are using f+"walkin.txt" can you add a File to a String? Will that code actually compile?


Yes, it will compile because by doing f+"" we are actually casting the f Object to String.

Will Object1.toString.compareTo(object2) compile? Because the compareTo method of String is parametrised to String it won't accept Object as a parameter.


Yes, it is working. ArrayList al.get(int x) - returns the element Object in the al at the position x. We are storing textFile to the al, which is a String.
The method "public int compareTo(Object o)" Compares this String to another Object. If the Object is a String, this function behaves like compareTo(String). Otherwise, it throws a ClassCastException (as Strings are comparable only to other Strings).
You can refer to http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.Object). I think, that is the reason it is compiling and generating output successfully (I am not quit sure).
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!