0

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 = %d\n", i + 'A', count[i]);

        }

        in.close();
    }
}
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Nicholas
  • 89
  • 6
  • 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 (https://docs.oracle.com/javase/8/docs/api/java/util/Map.html) ? – Koray Tugay Nov 25 '18 at 01: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 02: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 02 '18 at 14:08
  • Same for several of your other questions on this site. – Hovercraft Full Of Eels Dec 02 '18 at 14:09

1 Answers1

1

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);
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319