用于huffman_test的 Decode() 方法中的错误 --> ascii_text



我是一名初学者Java程序员,正在尝试编写一个Decode((方法,该方法应该在霍夫曼树中使用编码的(下面包含的Encode(((huffman_text,并将其解码回ascii_text。我想我已经差不多了,但我遇到了一些编译错误,主要是关于charAt的使用,但不知道用什么代替charAt来让我的代码工作。

如果有人知道如何进行,我将不胜感激。

错误

Tree.java:96:错误:找不到符号if(huffman_text.chatAt(i(=="1"(^symbol:方法chartAt(int(location:字符串类型的变量huffman_text

Tree.java:103:错误:找不到符号if(root.carAt(i(.length((==1(^符号:变量i位置:类别树

Tree.java:104:错误:找不到符号ascii_text+=root.charAt(i(;^符号:变量i位置:类别树

Tree.java:105:错误:找不到符号否则如果(leaf_nodes[huffman_code]==null(^符号:变量huffman_code位置:类别树

Tree.java:107:错误:找不到符号huffman_text.charAt(i((;^符号:变量i位置:类别树

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);
}
}

我将逐一检查错误。

错误行96
cannot find symbol if (huffman_text.chartAt(i) == "1") ^ symbol: method chartAt(int)

Typo:方法被称为charAt()而不是chartAt()

错误行103、104和107
cannot find symbol { ... } ^ symbol: variable i

您在for循环之外使用了i:它没有在此范围中声明

错误行105
cannot find symbol else if (leaf_nodes[huffman_code] == null) ^ symbol: variable huffman_code

huffman_code未在任何位置声明。

这些都是错误,你应该能够解决。或者至少只要看着他们就知道该怎么办。理解错误消息是编程中需要的技能!

无论如何,你的代码还有一些问题,实际上也应该抛出错误消息:

if (huffman_text.chartAt(i) == "1")在本语句中,您将比较huffman_text.chartAt(i)"1"。这应该是不可能的,因为方法charAt()返回一个char,但"1"的类型是String
比较这一点的正确方法是只使用表示char:
if (huffman_text.chartAt(i) == '1')的单引号

相关内容

  • 没有找到相关文章

最新更新