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

标签 java data-structures ascii decode huffman-code

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

如果有人知道如何继续,我将不胜感激。

错误

Tree.java:96: 错误: 找不到符号 if (huffman_text.chartAt(i) == "1") ^ 符号:方法chartAt(int) 位置:String类型的变量huffman_text

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

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

Tree.java:105: 错误: 找不到符号 else if (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);
    }
}


最佳答案

我将一一检查错误。

Error line 96
cannot find symbol if (huffman_text.chartAt(i) == "1") ^ symbol: method chartAt(int)

拼写错误:方法被称为 charAt() 不是 chartAt()

Error lines 103, 104 and 107
cannot find symbol { ... } ^ symbol: variable i

您在 for 循环之外使用了 i:它未在此范围内声明

Error line 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')

关于java - huffman_test --> ascii_text 的 Decode() 方法中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61277666/

相关文章:

java - 如何仅使用给定的格式将给定的字符串转换为java中的日期?

c# - 使用 LINQ 合并 2 个数组

c++ - 在链表中插入 'n' 节点并打印其数据(C++)

python - 解码 Flask request.form 上的非 ASCII 字符

java - 如何在 Spring Boot 中从 Active Directory LDAP 服务器获取所有组用户?

c# - 计算机视觉 : Segmentation of XRay image vertebrae detection

java - 在Java中如何根据单词的长度来定义猜测的次数?

java - 获取数组中连续元素的绝对差值,并返回哪个差值出现最多

java - 凯撒密码帮助 ASCII 循环

php - 带有 ascii 图标的文本的 Mysql 列类型