aspose file tools*
The moose likes Beginning Java and the fly likes My head is about to explode! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My head is about to explode!" Watch "My head is about to explode!" New topic
Author

My head is about to explode!

Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Hi guys, how is it going?!

In short, I've got a cipher code, and all I need to do is to:

1-count the occurrences for each letter
2-replace the most frequent letter with 'E', the second with 'T' and so on.

Fairly simple!

However, what I have done is :



so, my problem is that once I finish counting how many times each letter appeared, I don't know what to do next because of the changing of 'freq' array, which's got each index corresponds to an alphabetic one (0,A),(1,B)...and so on

please enlighten me to improve this code
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Hi,
Welcome to javaranch. This prolem was discussed in the forum several times, so I advice you to do a search on the subject here in the forum.
Think a little bit about the sorting part. You stored the occurence count for all the letters in your array. You know that freq[0] belongs to Alpha[0], freq[1] belongs to Alpha[1], ....
After counting you reorder the freq array. You missed one thing here. What do you want to achieve by reordering the array? Think about it and you will see what I'm talking about.
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Thanks Miklos

I believe that you're talking about I have missed each letter number of occurrences !?
This is my nightmare that I could do!
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
I mean, you sort the freq array to order the letters based on the count of their occurences which is right. But you just mix up the freq array and forgot to order the alphabet according to that.
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
WOW! I never thought of that!
I'll have a go, thank you
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
I give up

any further hints?
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
You give up really easy. Forget about Java, just concentrate on the algorithm. Write down the algorithm, and when it's okay you can start to implement it in Java. We can help with that,but we won't give the soultion instead of you;)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
And if you want answers, in future please note this FAQ.
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
I really appreciate your collaboration, and believe me I'm not looking for a ready-solution ..and to show my good intentions let me tell you what I am thinking about,algorithm-wise not java:

1-Create a two-dimensional array arr[26][2], the first column is for holding indices from 0-25, and the other one is holding the frequencies for each letter taking the values from "freq" array ,e.g: a[0][0]=0-->A & a[0][1]= 10 times of occurrences

2-After that we sort the 2-dimensional array according to the 2nd column which holds the occurrences number, so we still hold the index for every letter,and easily we know where the 'A' went and other letters

3-Now I got lost!!
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Campbell Ritchie wrote:And if you want answers, in future please note this FAQ.


Sorry, cheer up mate
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
This what I could come up with! it didn't work though!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Please have a look at the administrative private message I sent a minute or so ago.

[Pedantic mode]There is no such thing as a 2D array in Java. You only get arrays of arrays, which are more versatile[/Pedantic mode]

You only need one array for counting frequencies, if you are using arrays. No need for two arrays. You simply need a frequency array, and you can increment the elements as you pass the letters. That bit is really easy. You probably don't need the ABCD... array either.
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Campbell Ritchie wrote:Please have a look at the administrative private message I sent a minute or so ago.

[Pedantic mode]There is no such thing as a 2D array in Java. You only get arrays of arrays, which are more versatile[/Pedantic mode]

You only need one array for counting frequencies, if you are using arrays. No need for two arrays. You simply need a frequency array, and you can increment the elements as you pass the letters. That bit is really easy. You probably don't need the ABCD... array either.


Thanks a lot, the counting bit is easy, but my problem still in how I can hold the index of each frequency! in order to replace its corresponding letter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
I take your point. You need some way to link letters to frequencies.

Set up a class which incorporates a letter, a frequency and a replacement letter. Put those in the array from A to Z. Remember a char is not a character at all, despite what people say. It is a number. You can do arithmetic on it. Try this:So you can use each letter to access the array and call its increment() method.
Make your class implement the Comparable interface, and simply subtract the two frequencies in the compareTo() method.
Then set the replacement character after sorting, and comparison with the frequencies. According to this Oxford English Dictionary website, that is EARIOTNSLCUDPMHGBFYWKVXZJQ. But you have been given a different set of frequencies, which you had best use.
Then use that replacement in your original String.
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Campbell Ritchie wrote:I take your point. You need some way to link letters to frequencies.

Set up a class which incorporates a letter, a frequency and a replacement letter. Put those in the array from A to Z. Remember a char is not a character at all, despite what people say. It is a number. You can do arithmetic on it. Try this:So you can use each letter to access the array and call its increment() method.
Make your class implement the Comparable interface, and simply subtract the two frequencies in the compareTo() method.
Then set the replacement character after sorting, and comparison with the frequencies. According to this Oxford English Dictionary website, that is EARIOTNSLCUDPMHGBFYWKVXZJQ. But you have been given a different set of frequencies, which you had best use.
Then use that replacement in your original String.



I really appreciate your hints, but I still can't do it!
Especially I don't know what to compare with each other!?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
I tried it last night with both the frequency tables on that Oxford website, and got a decrypted output . . . and couldn't understand it

If you want to get the index of your 26-element array, try myLetters['z' - 'a'].
Create a simple method which demonstrates an array like that.
Then go through your coded String, and for each of the letters, increment the count in your array. You can even iterate through the array like thisSee if you can get that bit working. See whether that helps at all. Note you can use upper-case and lower-case versions of that for loop.
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Campbell Ritchie wrote:I tried it last night with both the frequency tables on that Oxford website, and got a decrypted output . . . and couldn't understand it

If you want to get the index of your 26-element array, try myLetters['z' - 'a'].
Create a simple method which demonstrates an array like that.
Then go through your coded String, and for each of the letters, increment the count in your array. You can even iterate through the array like thisSee if you can get that bit working. See whether that helps at all. Note you can use upper-case and lower-case versions of that for loop.


If you really interested in knowing the whole thing , here you go:
ZYEMOUSOUSIZWYIXOKSOTZQSOBSYLSWDKHTMOWARMSKSRRSILISFESWBHSTOISQISRRZWSOIRYRMSOUSIODSTLYISWDKHTMOWAWYEWBYXXYWSWDKHTMGYIATOISQISTSWRRMSYRMSIRSVRTGHKKWYRJSTYSOTZOWAXOZJSMOIARYAYJISONHWDRMHTBHQMSIJZMOWATMYEKAWYRJSAHLLHBEKRRYAYMYGSUSIRMHTSVSIBHTSOTNTZYERYGIHRSOQIYDIOXRMORORROBNTBHQMSITOERYXORHBOKKZOWAJISONTRMSXGHRMYEROETSIMOUHWDRYPEXQHWOWAXONSOBMOWDSOTHXQKSJIERSLYIBSOQQIYOBMHTWYRWSBSTTOIHKZRMSWHBSTRGOZRYAYWSRMHTRMSRIHBNRYAYHWDRMHTGHKKJSRYASRSBRGMSWRMSTEJTRHRERHYWHTWYRIHDMROWARYLHWAOJSRRSIYWSETHWDRMSLISFESWBHSTRYDEHASZYEDYYAKEBN

and thanks for the help, I'll try it
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40



When you swap the freqs, shouldn't you swap the letters too? After all, the letter and freqs should stay together right?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
Henry Wong wrote:

When you swap the freqs, shouldn't you swap the letters too? After all, the letter and freqs should stay together right?

Henry


Thanks a lot, I should not miss that!
lisa broker
Greenhorn

Joined: Oct 12, 2009
Posts: 2
I just wanna say one thing, programming require patient a lot, keep going


What do you mean, I ain't kind?
Abdulmalik Malik
Ranch Hand

Joined: May 15, 2008
Posts: 31
lisa broker wrote:I just wanna say one thing, programming require patient a lot, keep going


Exactly, and this what I lack, I'm afraid!
But by staying in touch, with people as such, I should become patient very much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My head is about to explode!