Problem with tracking word frequency[Java] - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Problem with tracking word frequency[Java] (2979 Views)
| Problem with tracking word frequency[Java] by mashnino(op): 6:50pm On Dec 28, 2012 |
I have been battling with this code for days now i can't seem to get it to work the way it is suppose to... I already have it working but not the way it suppose to. The code is suppose to read a text file.. then count how many times each word appears in the text file.. Then an ASSOCIATION CLASS object, which takes the "word" and it's "frequency(the numba of times it appears)" as an arguement i.e KEY AND VALUE. The pair is put in a "CONTAINER CLASS". I kind of pulled true but my problem now is; the words are repeated and the frequency has gone out of hand i don't even understand how the frequency works sef.. Files MAIN CLASS: package ics202.lab02;and this is the text file DhahranNB:the words in the text file cannot be more than 100 I am a 300level computer science student and this is part of our lab work assignment the topic we are actually treating is data structures |
| Re: Problem with tracking word frequency[Java] by Javanian: 8:47pm On Dec 28, 2012 |
First of all, Next time please don't direct the problem to me, i have a life outside Nairaland you know and i may not always be available to help.Next time you want to read from a text file use the classes in the java.io package and not the scanner class. I wasn't able to re write the entire code beacause i didn't see all your classes, but we can still work out something. if i am right this is where you have problem with your code. Now i wrote a new class that handles this problem so it would be easy to re use my code in yours
|
| Re: Problem with tracking word frequency[Java] by mashnino(op): 8:54pm On Dec 28, 2012 |
I am so sorry for that... Lemme check it out... would let you know if it works |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 9:21pm On Dec 28, 2012 |
The problem with BufferedReader is that, it doesn't have a method that checks if there's a next word. and i used the "readLine()" to convert the text file to a string, i don't know if i am suppose to do so |
| Re: Problem with tracking word frequency[Java] by Javanian: 9:29pm On Dec 28, 2012 |
^^^ There are a lot of ways you can work around that code, but if you are to follow my implementation, you have to read the text file into a String... |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 9:33pm On Dec 28, 2012 |
to just make the whole thing easier... just show me a piece of code i can use to check the number of times a word appears in a text file... using Scanner...i will connect the whole code together myself.. cos dats just the part that is giving me problem... |
| Re: Problem with tracking word frequency[Java] by Javanian: 9:44pm On Dec 28, 2012 |
But thats what i gave you now, all you need to do is read the text file to String, that isn't difficult now, or is that what you need? |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 9:53pm On Dec 28, 2012 |
jez did that... the value of "value" doesn't change... unlike Scanner class, bufferedReader don't have the "hasNext()" and "next()" Method.. that's where my problem is from now... |
| Re: Problem with tracking word frequency[Java] by Javanian: 9:55pm On Dec 28, 2012 |
Why don't you just stick to the Scanner class to avoid complicating issues... |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 9:58pm On Dec 28, 2012 |
ok...lemme try it around with the scanner class den... |
| Re: Problem with tracking word frequency[Java] by segsalerty(m): 11:00pm On Dec 28, 2012 |
Just passing by ........... this sounds like an ACM trouble? Wat sch abeg? :DPost d whole problem and test sample input and output so we go fit brainstorm together better than reading ur code |
| Re: Problem with tracking word frequency[Java] by csharpjava(m): 12:05am On Dec 29, 2012 |
If you don't mind me intruding as this is meant for Javanian, then I think what you need is a Java map, see example below from mkyong.com/java/how-to-loop-a-map-in-java which you can modify. import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class LoopMap { public static void main(String[] args) { // initial a Map Map<String, String> map = new HashMap<String, String>(); map.put("1", "Jan" ;map.put("2", "Feb" ;map.put("3", "Mar" ;map.put("4", "Apr" ;map.put("5", "May" ;map.put("6", "Jun" ;System.out.println("Example 1..." ;// Map -> Set -> Iterator -> Map.Entry -> troublesome Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry mapEntry = (Map.Entry) iterator.next(); System.out.println("The key is: " + mapEntry.getKey() + ",value is :" + mapEntry.getValue()); } System.out.println("Example 2..." ;// more elegant way for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } System.out.println("Example 3..." ;// weired way, but work anyway for (Object key : map.keySet()) { System.out.println("Key : " + key.toString() + " Value : " + map.get(key)); } } } |
| Re: Problem with tracking word frequency[Java] by Fayimora(m): 12:58am On Dec 29, 2012 |
As already stated, please post your problems without directing it at anyone. You just end up putting folks off. I've changed the topic |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 2:45am On Dec 29, 2012 |
fank you fayimora...i ve alwais wanted to be here alwais but it seemed like there was no java person... so sorry about that |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 2:47am On Dec 29, 2012 |
csharpjava: If you don't mind me intruding as this is meant for Javanian, then I think what you need is a Java map, see example below from mkyong.com/java/how-to-loop-a-map-in-java which you can modify.This MAP class is almost the same thing as the Association class in which i am working with.. |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 2:50am On Dec 29, 2012 |
This is the initial question Complete the program TestAssociation.java to associate each word in a text file withand defaultly the TEST ASSOCIATION class looks like this package ics202.lab02;and mine is above in which i haven't completed...got stuck in the same position as i explained above... |
| Re: Problem with tracking word frequency[Java] by Nobody: 5:24am On Dec 29, 2012*. Modified: 8:49am On Apr 07, 2013 |
| Re: Problem with tracking word frequency[Java] by segsalerty(m): 11:41am On Dec 29, 2012 |
mashnino: fank you fayimora...i ve alwais wanted to be here alwais but it seemed like there was no java person...Are u kidding me? I contributed, got no response, smh.......... thats really hw to learn! Kudos..... ![]() |
| Re: Problem with tracking word frequency[Java] by segsalerty(m): 11:55am On Dec 29, 2012 |
Anywayz, i changed my mind.... wat ur teacher wants to see is hw u will implement a Map. Store words as Map<String, WordClass>, After reading/scanning each word in d text file, use map function that checks if key exist in d map. If yes..... get the WordClass value and increament its counter/occurence property. Else. Put into d map a new key(the new word) and new instance of (wordclass). With these, u will fulfil d task given. Here is a simple skelenton of ur WordClass cass Properties String word; int counter or occurence; Constructor WordClass(String w){ Word=w; Occurence=0; } Increment(){ Occurence++; }// call dis when such word exist in map Other fuctioms u need are ur getters. Hope dis helps solve ur assignment |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 1:18pm On Dec 29, 2012 |
segsalerty: Are u kidding me? I contributed, got no response, smh.......... thats really hw to learn! Kudos.....lol..sorry man..ahn ahn don't take it personal now... |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 1:26pm On Dec 29, 2012 |
Okay guys.. seriously speaking i am lost within the codes you guys are dropping. let me post the output of my own code maybe you guys can finally fish out where i am not getting it right { Dhahran , 0 }and my code is up there, don't wanna post it again so that it won't fill up the whole place.. and this is the text file that must not have more than hundred words DhahranTo just make the whole problem short... If you guys can just help me with a method that counts the number of times each word in a text file appears in that same text file.. assuming you are using a scanner class to read the words from the text file.. NB: I can't deviate from the skeleton that was given, that's why i can't use the MAP class.. I think that's why the assignment is this complicated. If you also notice the code skips some words from the text file. I don't know why that is that.. |
| Re: Problem with tracking word frequency[Java] by segsalerty(m): 2:17pm On Dec 29, 2012 |
Zip wats needed abt d assignment and send to my email. Segsalerty@yahoo.com will look into when i get on system. mashnino: lol..sorry man..ahn ahnLolz, i gats kpara for u nau! Wondering where u got ur fact frm...... Elm, less i forget.... i go like to toast javanian if toastable, make i knw her first coz all i see on dis section is javanian here,javanian there...... winks |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 2:20pm On Dec 29, 2012 |
^^ d chick bad gan.. |
| Re: Problem with tracking word frequency[Java] by segsalerty(m): 2:24pm On Dec 29, 2012 |
mashnino: ^^ d chick bad gan..Seen her before? Abeg give me her facebook name make i peep! Send am too to my email ![]() |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 3:59pm On Dec 29, 2012 |
Just sent you the file...^^^ |
| Re: Problem with tracking word frequency[Java] by segsalerty(m): 4:49pm On Dec 29, 2012 |
public class TestAssociation{sorry, i had to rush this, clean up ur code, nepa is bringing and taking light.... bt this concept works fine thou there are flaws in d design of the classwork libraries (some functions are missing thats suppose to be there in "MySearchableContainer" to make it more efficient) well, it works fine this way, ask me anything u dnt understand, will respond via mobile. NOTE : [s]output is nt 100% right, bt its smth u can fix [/s] Light is back, output is 100% correct now, so, just recopy paste and rerun |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 5:37pm On Dec 29, 2012 |
Shit shit shit!!!!!!! I have mixed feelings here... i am sad because i culdn't do it i am happy because i have learnt some new stuffs... I am gonna sit down and study the stuffs... THANKS to everybody that contributed..i really appreciate.. THANK YOU GUYS.. "jumping around my room" |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 5:46pm On Dec 29, 2012 |
Please do me all of us here a favor.. Xplain where my mistake is from so that anybody that comes accross this topic would benefit... Jez for the house... Fanx so much |
| Re: Problem with tracking word frequency[Java] by segsalerty(m): 5:46pm On Dec 29, 2012 |
mashnino: Shit shit shit!!!!!!! Good for u Boss .... wish our institutions can give these tasks in Nigeria. so bad and poor of our school NB: i still get interest ontop this javanian o, why is she ignoring my interest nw ![]() haba, i will go on Nairaland strike again if she no respond o! |
| Re: Problem with tracking word frequency[Java] by Nobody: 6:19pm On Dec 29, 2012*. Modified: 8:49am On Apr 07, 2013 |
| Re: Problem with tracking word frequency[Java] by segsalerty(m): 6:34pm On Dec 29, 2012 |
mashnino: Please do me all of us here a favor..Do that urself sir ![]() |
| Re: Problem with tracking word frequency[Java] by mashnino(op): 6:56pm On Dec 29, 2012 |
Ok... Will xplain... Laptop dead.. No light on my mobile phone... |
How To Create A Shipping Tracking On Website.? • I Have A Problem With Raise Event In C# • 2 • 3 • 4
Where Can I Learn Programming In Lagos? • Microsoft Launches Silverlight: An Alternative To Flash • Yii2 Rest API Question
;
and i may not always be available to help.
:DPost d whole problem and test sample input and output
