简体   繁体   中英

Stack overflow when using recursion for a morsecode translator

I've been trying to set up a translator by using a binary tree to set up the morsecode's structure. I consistently receive a stack overflow error when re-executing the method

private String decode(String pCode, BinaryTree<String> pTree)
    {
        int loop1 = 0;
        int loop2 = 0;
        BinaryTree<String> tree = morsetree;
        if (!pCode.isEmpty())
        {
            if (loop2 <= pCode.length())
            {
                while (pCode.substring(loop, loop+1) == " ")
                {
                    loop2++;
                }
            }
            if (loop2 > 5)
            {
                return null;
            }
            for (int i=0;i<loop2;i++)
            {
                if (pCode.substring(i, i+1) == "-")
                {
                    tree = tree.getRightTree();
                }
                else
                {
                    tree = tree.getLeftTree();
                }
            }
            System.out.println(tree.getContent());
            return getLetter(pCode.substring(loop2, pCode.length())
                                                           ,morsetree);
        }
        return null;
    }

Havent read the whole code but this part alone seems problematic.

while (pCode.substring(loop, loop+1) == " ")
            {
                loop2++;
            }

Where was “loop” defined? How do you assure that the while loop ends? It seems like the condition wouldnt produce a different result after loop runs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM