Please Help my code for this program is listed below what the program is supposed to do is also listed below. I can't get it to work.

The problem

Write a program to compress a message consisting of the ten symbols A, B, C, D, E, F, G, H, I, and J. You should create a variable-length code for each symbol to replace the traditional 8-bit ASCII code. Your code should have the “prefix property discussed in Section 4.6 (no code symbol can appear at the beginning of any other). Huffman’s algorithm will give you a code of optimal length. (When you build your subtrees in the algorithm, put the previous subtree of smaller weight on the left, so that everyone will get the same answer). If you input a string of characters, the program should output a list of each symbol followed by its frequency and code, and the compression string. The input string GGABBHGHGCHDHGFHGFBAGEDGDHFFEHGAABCBGBAAHHEEHFHFBA A, for example, would yield the table

Letter Frequency Code

A 8 110

B 7 101

C 2 11110

D 3 11111

E 4 1110

F 6 100

G 10 00

H 11 01

I 0 –

J 0 –

And the compression string would begin 00001101011010100010011110 . . .

Again any help you can give would be great ASAP.

My code

import java.util.*;

public class HuffmanTree2 {
BinaryTree htree;
int frequency[];
String alphabet;
String codes[];

public HuffmanTree2 (FileReader inFile) throws Exception {
int charSet[] = new int[128];

for (int i=0; i<128; i++)
charSet = 0;

while (inFile.ready())

int count=0;
for (int i=0; i<128; i++)
if (charSet > 0) count++;

frequency = new int[count];
codes = new String[count];
alphabet = "";

count = 0;
for (int i=0; i<128; i++){
if (charSet > 0) {
frequency[count] = charSet;
alphabet = alphabet + (char)i;

htree = makeTree(frequency, alphabet);
setCodes(htree, "");

public HuffmanTree2 (int frequencies[], String initAlphabet){
frequency = new int[initAlphabet.length()];
alphabet = new String(initAlphabet);
codes = new String[initAlphabet.length()];

htree = makeTree(frequency, alphabet);
setCodes(htree, "");

public BinaryTree makeTree (int frequencies[], String initAlphabet){
SortedSet sortedTrees = new TreeSet();
BinaryTree tree1, tree2, newTree;
LetterFreq2 value1, value2;

for (int i=0; i<frequencies.length; i++){
if (frequencies > 0)
sortedTrees.add(new BinaryTree(new LetterFreq2(initAlphabet.charAt(i),

while (sortedTrees.size() > 1){
tree1 = (BinaryTree)sortedTrees.first();
tree2 = (BinaryTree)sortedTrees.first();

value1 = (LetterFreq2)tree1.getRootItem();
value2 = (LetterFreq2)tree2.getRootItem();

newTree = new BinaryTree(new LetterFreq2('*', value1.freq+value2.freq));
return (BinaryTree)sortedTrees.first();

private void setCodes(BinaryTree currentTree, String currentCode){
if (currentTree.isLeaf()){
LetterFreq2 value = (LetterFreq2)currentTree.getRootItem();
int pos = alphabet.indexOf(value.letter);
codes[pos] = currentCode;
else {
setCodes(currentTree.getLeftTree(), currentCode+"0");
setCodes(currentTree.getRightTree(), currentCode+"1");
public void printCodes(){
for (int i=0; i<codes.length; i++)
System.out.println(alphabet.charAt(i) + ": " + codes);

public String[] getCodes(){
String newCodes[] = new String[codes.length];
for (int i=0; i<codes.length; i++)
newCodes = codes;
return newCodes;
public String getAlphabet(){
return new String(alphabet);
public FileWriter encryptFile(FileReader inFile, String outFileName) throws
char ch;
int pos;
int outChar=0;
int bitCount = 0;

FileWriter outFile = new FileWriter(outFileName);

while (inFile.ready()) {
ch = (char);
pos = alphabet.indexOf(ch);
for (int i=0; i<codes[pos].length(); i++){
if (bitCount >= 32){
outChar = 0;
bitCount = 0;
outChar = (outChar << 1) | (int)codes[pos].charAt(i);
if (bitCount > 0){
outChar = outChar << (32 - bitCount);
return outFile;

If you put code tags around your code I could help you more.

Untill then, take a look at the API. An easy way to compress a String is replace characters with multiple occurences with a number representation of the occurences, and then the letter.

Sounds like Huffman compression to me. Also sounds like a homework problem. I'd still try to help since it looks like you've tried the problem at least, but its too hard to read without code tags