Sorting array of letter frequency
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am writing a program that counts the letter frequency of a file to eventually use to help decode a different file. I am able to correctly sort the array by values (the method call i commented out) but am unable to figure out how to sort the corresponding letters with the values. Any help would be greatly appreciated.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
class CodeBreakerProject {
public static void swap(int count, int index1, int index2) {
int temp = count[index1];
count[index1] = count[index2];
count[index2] = temp;
}
public static void selectionSort(int count) {
for (int i = 0; i < count.length; i++) {
// find smallest element from i to end
int minIndex = i; // assume 1st element is smallest
for (int j = i; j < count.length; j++) {
if (count[j] > count[minIndex]) {
minIndex = j;
}
}
swap(count, i, minIndex);
}
}
public static void main(String args) throws IOException {
File file1 = new File("training.txt");
BufferedReader in = new BufferedReader(new FileReader(file1));
System.out.println("Letter Frequency");
int nextChar;
char ch;
int count = new int[26];
while ((nextChar = in.read()) != -1) {
ch = ((char) nextChar);
if (ch >= 'a' && ch <= 'z') { count[ch - 'a']++; }
}
//selectionSort(count); this sorts the values but does not move the letter assignments
for (int i = 0; i < 26; i++) {
System.out.printf("%c = %dn", i + 'A', count[i]);
}
in.close();
}
}
java drjava
add a comment |
I am writing a program that counts the letter frequency of a file to eventually use to help decode a different file. I am able to correctly sort the array by values (the method call i commented out) but am unable to figure out how to sort the corresponding letters with the values. Any help would be greatly appreciated.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
class CodeBreakerProject {
public static void swap(int count, int index1, int index2) {
int temp = count[index1];
count[index1] = count[index2];
count[index2] = temp;
}
public static void selectionSort(int count) {
for (int i = 0; i < count.length; i++) {
// find smallest element from i to end
int minIndex = i; // assume 1st element is smallest
for (int j = i; j < count.length; j++) {
if (count[j] > count[minIndex]) {
minIndex = j;
}
}
swap(count, i, minIndex);
}
}
public static void main(String args) throws IOException {
File file1 = new File("training.txt");
BufferedReader in = new BufferedReader(new FileReader(file1));
System.out.println("Letter Frequency");
int nextChar;
char ch;
int count = new int[26];
while ((nextChar = in.read()) != -1) {
ch = ((char) nextChar);
if (ch >= 'a' && ch <= 'z') { count[ch - 'a']++; }
}
//selectionSort(count); this sorts the values but does not move the letter assignments
for (int i = 0; i < 26; i++) {
System.out.printf("%c = %dn", i + 'A', count[i]);
}
in.close();
}
}
java drjava
How is the goalcounts the letter frequency of a file
is related tohow to sort the corresponding letters with the values
? Why are you not using a Map (docs.oracle.com/javase/8/docs/api/java/util/Map.html) ?
– Koray Tugay
Nov 25 '18 at 1:51
Sorry, i probably didn't explain it to well. My goal is to find the letter frequency of a file to use as a key to decode a different file. The letters with the highest frequencies in my "training" file will be swapped with highest frequency letters in an "encrypted" file provided to me. In the English language, for instance, the letter “e” is the most common in almost any block of text of reasonable length. As i am working with huge text files, this method should decode pretty well, but not perfectly. I haven't learned how to map yet though. Still new to programming.
– Nicholas
Nov 25 '18 at 2:24
1
You appear to have ignored @KorayTugay's answer. You may wish to acknowledge the answer in some way, and certainly up-vote it and accept it if it helped you, else his volunteer efforts have been for naught.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:08
Same for several of your other questions on this site.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:09
add a comment |
I am writing a program that counts the letter frequency of a file to eventually use to help decode a different file. I am able to correctly sort the array by values (the method call i commented out) but am unable to figure out how to sort the corresponding letters with the values. Any help would be greatly appreciated.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
class CodeBreakerProject {
public static void swap(int count, int index1, int index2) {
int temp = count[index1];
count[index1] = count[index2];
count[index2] = temp;
}
public static void selectionSort(int count) {
for (int i = 0; i < count.length; i++) {
// find smallest element from i to end
int minIndex = i; // assume 1st element is smallest
for (int j = i; j < count.length; j++) {
if (count[j] > count[minIndex]) {
minIndex = j;
}
}
swap(count, i, minIndex);
}
}
public static void main(String args) throws IOException {
File file1 = new File("training.txt");
BufferedReader in = new BufferedReader(new FileReader(file1));
System.out.println("Letter Frequency");
int nextChar;
char ch;
int count = new int[26];
while ((nextChar = in.read()) != -1) {
ch = ((char) nextChar);
if (ch >= 'a' && ch <= 'z') { count[ch - 'a']++; }
}
//selectionSort(count); this sorts the values but does not move the letter assignments
for (int i = 0; i < 26; i++) {
System.out.printf("%c = %dn", i + 'A', count[i]);
}
in.close();
}
}
java drjava
I am writing a program that counts the letter frequency of a file to eventually use to help decode a different file. I am able to correctly sort the array by values (the method call i commented out) but am unable to figure out how to sort the corresponding letters with the values. Any help would be greatly appreciated.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
class CodeBreakerProject {
public static void swap(int count, int index1, int index2) {
int temp = count[index1];
count[index1] = count[index2];
count[index2] = temp;
}
public static void selectionSort(int count) {
for (int i = 0; i < count.length; i++) {
// find smallest element from i to end
int minIndex = i; // assume 1st element is smallest
for (int j = i; j < count.length; j++) {
if (count[j] > count[minIndex]) {
minIndex = j;
}
}
swap(count, i, minIndex);
}
}
public static void main(String args) throws IOException {
File file1 = new File("training.txt");
BufferedReader in = new BufferedReader(new FileReader(file1));
System.out.println("Letter Frequency");
int nextChar;
char ch;
int count = new int[26];
while ((nextChar = in.read()) != -1) {
ch = ((char) nextChar);
if (ch >= 'a' && ch <= 'z') { count[ch - 'a']++; }
}
//selectionSort(count); this sorts the values but does not move the letter assignments
for (int i = 0; i < 26; i++) {
System.out.printf("%c = %dn", i + 'A', count[i]);
}
in.close();
}
}
java drjava
java drjava
edited Nov 25 '18 at 1:48
Koray Tugay
9,21828120229
9,21828120229
asked Nov 25 '18 at 1:44
NicholasNicholas
62
62
How is the goalcounts the letter frequency of a file
is related tohow to sort the corresponding letters with the values
? Why are you not using a Map (docs.oracle.com/javase/8/docs/api/java/util/Map.html) ?
– Koray Tugay
Nov 25 '18 at 1:51
Sorry, i probably didn't explain it to well. My goal is to find the letter frequency of a file to use as a key to decode a different file. The letters with the highest frequencies in my "training" file will be swapped with highest frequency letters in an "encrypted" file provided to me. In the English language, for instance, the letter “e” is the most common in almost any block of text of reasonable length. As i am working with huge text files, this method should decode pretty well, but not perfectly. I haven't learned how to map yet though. Still new to programming.
– Nicholas
Nov 25 '18 at 2:24
1
You appear to have ignored @KorayTugay's answer. You may wish to acknowledge the answer in some way, and certainly up-vote it and accept it if it helped you, else his volunteer efforts have been for naught.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:08
Same for several of your other questions on this site.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:09
add a comment |
How is the goalcounts the letter frequency of a file
is related tohow to sort the corresponding letters with the values
? Why are you not using a Map (docs.oracle.com/javase/8/docs/api/java/util/Map.html) ?
– Koray Tugay
Nov 25 '18 at 1:51
Sorry, i probably didn't explain it to well. My goal is to find the letter frequency of a file to use as a key to decode a different file. The letters with the highest frequencies in my "training" file will be swapped with highest frequency letters in an "encrypted" file provided to me. In the English language, for instance, the letter “e” is the most common in almost any block of text of reasonable length. As i am working with huge text files, this method should decode pretty well, but not perfectly. I haven't learned how to map yet though. Still new to programming.
– Nicholas
Nov 25 '18 at 2:24
1
You appear to have ignored @KorayTugay's answer. You may wish to acknowledge the answer in some way, and certainly up-vote it and accept it if it helped you, else his volunteer efforts have been for naught.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:08
Same for several of your other questions on this site.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:09
How is the goal
counts the letter frequency of a file
is related to how to sort the corresponding letters with the values
? Why are you not using a Map (docs.oracle.com/javase/8/docs/api/java/util/Map.html) ?– Koray Tugay
Nov 25 '18 at 1:51
How is the goal
counts the letter frequency of a file
is related to how to sort the corresponding letters with the values
? Why are you not using a Map (docs.oracle.com/javase/8/docs/api/java/util/Map.html) ?– Koray Tugay
Nov 25 '18 at 1:51
Sorry, i probably didn't explain it to well. My goal is to find the letter frequency of a file to use as a key to decode a different file. The letters with the highest frequencies in my "training" file will be swapped with highest frequency letters in an "encrypted" file provided to me. In the English language, for instance, the letter “e” is the most common in almost any block of text of reasonable length. As i am working with huge text files, this method should decode pretty well, but not perfectly. I haven't learned how to map yet though. Still new to programming.
– Nicholas
Nov 25 '18 at 2:24
Sorry, i probably didn't explain it to well. My goal is to find the letter frequency of a file to use as a key to decode a different file. The letters with the highest frequencies in my "training" file will be swapped with highest frequency letters in an "encrypted" file provided to me. In the English language, for instance, the letter “e” is the most common in almost any block of text of reasonable length. As i am working with huge text files, this method should decode pretty well, but not perfectly. I haven't learned how to map yet though. Still new to programming.
– Nicholas
Nov 25 '18 at 2:24
1
1
You appear to have ignored @KorayTugay's answer. You may wish to acknowledge the answer in some way, and certainly up-vote it and accept it if it helped you, else his volunteer efforts have been for naught.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:08
You appear to have ignored @KorayTugay's answer. You may wish to acknowledge the answer in some way, and certainly up-vote it and accept it if it helped you, else his volunteer efforts have been for naught.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:08
Same for several of your other questions on this site.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:09
Same for several of your other questions on this site.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:09
add a comment |
1 Answer
1
active
oldest
votes
If I understand your ultimate goal, I do not think you need to sort the array you have. The first thing you must re-think is this. Do you really need to sort this array you have that is the frequencies of the characters you have in your training text?
Your training array, again if I understand correctly, resembles something like this:
int training = new int[5];
training[0] = 1; // a
training[1] = 2; // b
training[2] = 3; // c
training[3] = 2; // d
training[4] = 8; // e
Now, in your target file, you will again collect an array like this, but this time, for example the index 0 will have the largest number, for example:
// target array:
// [8, 2, 2, 3, 1]
Now all you have to do is to replace all character a
's with e
's in your target file. (since you found out by seeing that it has the highest frequency, and in your training the highest frequency was letter e
).
Why do you even need to sort your training array?
If I am understanding you wrong, maybe use a Map
to hold your frequency values, and look for any functionality you may need in that class:
Map<Character, Integer> characterFrequency = new HashMap<>();
characterFrequency.put('a', 4);
characterFrequency.put('b', 2);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463983%2fsorting-array-of-letter-frequency%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understand your ultimate goal, I do not think you need to sort the array you have. The first thing you must re-think is this. Do you really need to sort this array you have that is the frequencies of the characters you have in your training text?
Your training array, again if I understand correctly, resembles something like this:
int training = new int[5];
training[0] = 1; // a
training[1] = 2; // b
training[2] = 3; // c
training[3] = 2; // d
training[4] = 8; // e
Now, in your target file, you will again collect an array like this, but this time, for example the index 0 will have the largest number, for example:
// target array:
// [8, 2, 2, 3, 1]
Now all you have to do is to replace all character a
's with e
's in your target file. (since you found out by seeing that it has the highest frequency, and in your training the highest frequency was letter e
).
Why do you even need to sort your training array?
If I am understanding you wrong, maybe use a Map
to hold your frequency values, and look for any functionality you may need in that class:
Map<Character, Integer> characterFrequency = new HashMap<>();
characterFrequency.put('a', 4);
characterFrequency.put('b', 2);
add a comment |
If I understand your ultimate goal, I do not think you need to sort the array you have. The first thing you must re-think is this. Do you really need to sort this array you have that is the frequencies of the characters you have in your training text?
Your training array, again if I understand correctly, resembles something like this:
int training = new int[5];
training[0] = 1; // a
training[1] = 2; // b
training[2] = 3; // c
training[3] = 2; // d
training[4] = 8; // e
Now, in your target file, you will again collect an array like this, but this time, for example the index 0 will have the largest number, for example:
// target array:
// [8, 2, 2, 3, 1]
Now all you have to do is to replace all character a
's with e
's in your target file. (since you found out by seeing that it has the highest frequency, and in your training the highest frequency was letter e
).
Why do you even need to sort your training array?
If I am understanding you wrong, maybe use a Map
to hold your frequency values, and look for any functionality you may need in that class:
Map<Character, Integer> characterFrequency = new HashMap<>();
characterFrequency.put('a', 4);
characterFrequency.put('b', 2);
add a comment |
If I understand your ultimate goal, I do not think you need to sort the array you have. The first thing you must re-think is this. Do you really need to sort this array you have that is the frequencies of the characters you have in your training text?
Your training array, again if I understand correctly, resembles something like this:
int training = new int[5];
training[0] = 1; // a
training[1] = 2; // b
training[2] = 3; // c
training[3] = 2; // d
training[4] = 8; // e
Now, in your target file, you will again collect an array like this, but this time, for example the index 0 will have the largest number, for example:
// target array:
// [8, 2, 2, 3, 1]
Now all you have to do is to replace all character a
's with e
's in your target file. (since you found out by seeing that it has the highest frequency, and in your training the highest frequency was letter e
).
Why do you even need to sort your training array?
If I am understanding you wrong, maybe use a Map
to hold your frequency values, and look for any functionality you may need in that class:
Map<Character, Integer> characterFrequency = new HashMap<>();
characterFrequency.put('a', 4);
characterFrequency.put('b', 2);
If I understand your ultimate goal, I do not think you need to sort the array you have. The first thing you must re-think is this. Do you really need to sort this array you have that is the frequencies of the characters you have in your training text?
Your training array, again if I understand correctly, resembles something like this:
int training = new int[5];
training[0] = 1; // a
training[1] = 2; // b
training[2] = 3; // c
training[3] = 2; // d
training[4] = 8; // e
Now, in your target file, you will again collect an array like this, but this time, for example the index 0 will have the largest number, for example:
// target array:
// [8, 2, 2, 3, 1]
Now all you have to do is to replace all character a
's with e
's in your target file. (since you found out by seeing that it has the highest frequency, and in your training the highest frequency was letter e
).
Why do you even need to sort your training array?
If I am understanding you wrong, maybe use a Map
to hold your frequency values, and look for any functionality you may need in that class:
Map<Character, Integer> characterFrequency = new HashMap<>();
characterFrequency.put('a', 4);
characterFrequency.put('b', 2);
answered Nov 25 '18 at 2:55
Koray TugayKoray Tugay
9,21828120229
9,21828120229
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463983%2fsorting-array-of-letter-frequency%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How is the goal
counts the letter frequency of a file
is related tohow to sort the corresponding letters with the values
? Why are you not using a Map (docs.oracle.com/javase/8/docs/api/java/util/Map.html) ?– Koray Tugay
Nov 25 '18 at 1:51
Sorry, i probably didn't explain it to well. My goal is to find the letter frequency of a file to use as a key to decode a different file. The letters with the highest frequencies in my "training" file will be swapped with highest frequency letters in an "encrypted" file provided to me. In the English language, for instance, the letter “e” is the most common in almost any block of text of reasonable length. As i am working with huge text files, this method should decode pretty well, but not perfectly. I haven't learned how to map yet though. Still new to programming.
– Nicholas
Nov 25 '18 at 2:24
1
You appear to have ignored @KorayTugay's answer. You may wish to acknowledge the answer in some way, and certainly up-vote it and accept it if it helped you, else his volunteer efforts have been for naught.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:08
Same for several of your other questions on this site.
– Hovercraft Full Of Eels
Dec 2 '18 at 14:09