I'm a begginer Java programmer and am trying to write a Decode() method that is supposed to take an encoded (Encode() method included below) huffman_text within a Huffman tree and decode it back into ascii_text. I think I'm almost there but I am having some trouble with compilation errors, mostly regarding charAt usage, but don't know what to use instead of charAt to get my code to work.
I'd appreciate some help if anyone knows how to proceed.
ERRORS
Tree.java:96: error: cannot find symbol if (huffman_text.chartAt(i) == "1") ^ symbol: method chartAt(int) location: variable huffman_text of type String
Tree.java:103: error: cannot find symbol if (root.charAt(i).length() == 1) ^ symbol: variable i location: class Tree
Tree.java:104: error: cannot find symbol ascii_text += root.charAt(i); ^ symbol: variable i location: class Tree
Tree.java:105: error: cannot find symbol else if (leaf_nodes[huffman_code] == null) ^ symbol: variable huffman_code location: class Tree
Tree.java:107: error: cannot find symbol huffman_text.charAt(i)); ^ symbol: variable i location: class Tree
class Tree
{
Node root;
Node leaf_nodes[];
public void CreateLeafNodes(String ascii_text)
{
// Array of leaf nodes indexed by ASCII code
leaf_nodes = new Node[256];
// Parse text
for (int i = 0; i< ascii_text.length();i++)
{
// Get ASCII code for current character
int ascii_code = ascii_text.charAt(i);
// Create node if it does not exist
if (leaf_nodes[ascii_code] == null)
leaf_nodes[ascii_code] = new Node(ascii_code);
// Increment frequncy
leaf_nodes[ascii_code].frequency++;
}
}
public void BuildTree()
{
// Create heap
Heap heap = new Heap();
// Insert all leaf nodes
for (int i = 0; i < 256; i++)
if (leaf_nodes[i] != null)
heap.Insert(leaf_nodes[i]);
// Build tree
while(heap.GetLength() > 1)
{
// Extract 2 nodes with minimum frequency
Node left = (Node) heap.ExtractMax();
Node right = (Node) heap.ExtractMax();
// Create new node and make it the root for now
root = new Node(0);
root.left = left;
root.right = right;
root.frequency = left.frequency + right.frequency;
// Insert new node in heap
heap.Insert(root);
}
// Set Huffman codes
root.SetHuffmanCode("");
}
public String Encode(String ascii_text)
{
// Initialize result
String huffman_text = "";
// Traverse ASCII text
for (int i = 0; i < ascii_text.length(); i++)
{
// Get ASCII code
int ascii_code = ascii_text.charAt(i);
// Check if character is supported
if (leaf_nodes[ascii_code] == null)
throw new RuntimeException("Character not supported: " +
ascii_text.charAt(i));
// Get Huffman code
String huffman_code = leaf_nodes[ascii_code].huffman_code;
// Add it
huffman_text += huffman_code;
// Message
System.out.println(ascii_text.charAt(i) + " -> " + huffman_code);
}
// Result
return huffman_text;
}
public String Decode(String huffman_text)
{
// Initialize result
String ascii_text = "";
// Traverse huffman text
for (int i = 0; i < huffman_text.length(); i++)
{
while(root.left != null && root.right != null && i < huffman_text.length())
if (huffman_text.chartAt(i) == "1")
root = root.right;
else
root = root.left;
i++;
}
if (root != null)
if (root.charAt(i).length() == 1)
ascii_text += root.charAt(i);
else if (leaf_nodes[huffman_code] == null)
throw new RuntimeException("Character not supported: " +
huffman_text.charAt(i));
// Result
return ascii_text;
}
}
class Test
{
public static void main(String args[])
{
float compression;
Tree tree = new Tree();
if (args.length == 0)
throw new RuntimeException("Please enter an argument. ");
String ascii_text = args[0];
tree.CreateLeafNodes(ascii_text);
tree.BuildTree();
System.out.println(ascii_text);
String huffman_text = tree.Encode(ascii_text);
System.out.println(huffman_text);
compression = huffman_text.length() * 100/ascii_text.length();
System.out.println(compression);
String ascii_text_2 = tree.Decode(huffman_text);
}
}