Page 1 of 1

Some Assistance with Programming (Java)

Posted: Tue Oct 27, 2015 11:56 am
by UnScottable
First things first, thanks to the Cohhilition for being awesome! I really enjoy watching Cohh, and I decided that perhaps the Cohhilition could help me with my programming issue.

I'm working on a Dictionary in Java. Where a pair is comprised of a Key, and Value.

Key is a String, Value is an integer. What this dictionary will do is create these Pairs, from using a StringTokenizer that gets each unique word from a text. So by the end of this process, only unique words have a Key, and then the Value is the number of times this word appeared in the given text. We were given 4 texts to analyze.

I have successfully created these Key, Value pairs. And printed them to the screen, the only issue now is figuring out how to sort them. Once they are sorted I can then develop a histogram based on these texts, normalize the histogram, and then report the distance between two texts.

That's all fine and dandy, but how in the world would I go about sorting an array of Pair objects? I'm not sorting on array of Strings, but rather an array of these Pairs. I'm not sure how to go about sorting them, and my professor has refrained from explaining in detail because this is easy. Which I'm sure it will be to many people on here. I was looking around the internet and found several mentions of the Map, HashMap, and then Trees. But he says that we should use arrays. He also mentioned something about Generics being beneficial, but generics has never been demonstrated or explained to me. So I'm not quite sure how to proceed down that avenue.

Right now I'm not using generics at all. Just instantiating the Pair class, then putting the Pairs into an array. This array has its own class so that I can perform specific operations on this array.

I've been trying to keep going, but I realized it is essentially useless until I figure out how to sort them.

Once I figure out how to sort them, I have to calculate the distance between the two texts correctly. Which is a whole separate endeavor.

Any help or ideas is much appreciated! <3

Thanks!
Edward Jenkins

PS: If you would like to see the code then I will either send it to you, or post it in the comments. I figured I would refrain from typing my code in here, as this post is already pretty long. I'd be happy to send you the .java source file as well.

Posted: Thu Oct 29, 2015 8:25 am
by ticktock1231
Hello! Hope all is going well. I have to preface this with i don't program every day, and i haven't programmed in Java for quite some time. With that said, my idea would be to put the Key and Value in a 2d array, column[0] = Key and column[1] = value. From there it would be researching how to sort a 2d array. Stackoverflow.com has some really great people who can help with that. Doing a quick google search i found one that sorts based on a string of a 2d array.

Again I don't program a lot, but I know most basic and some advanced concepts. Hope this helps a little. Good luck!