Java substr 没有按预期工作

Java substr not working as expected

所以我正在 Java 中制作一个 brainfuck 解释器,除了 [ 和 ] 之外我没有任何问题。 (不可避免地)问题在于解析字符串。所以我处理循环的方法是找到两个括号之间的代码并调用函数(递归)重做括号内的代码。这在纸面上听起来不错,但 substring 不配合我。这是我处理 [

的地方
void openBracket(short i, String brainfuck)
    {
        /*LOGIC EXPLAINED: First set balance = 1, then loop through all characters after this,
         if another opening bracket is found increment balance, if a close bracket is found, 
         decrement balance. Also, when another close bracket is found test if balance is 1, if
         so then this is the proper bracket*/
        String codeSection = brainfuck.substring(i);
        short balance = 1;
        String codeToRedo;
        short endPoint = -1;

        for (short j = i; j < codeSection.length(); j++) 
        {
        //Check the character
        if (codeSection.charAt(j) == '[') 
        {
            balance++;
        } else if (codeSection.charAt(j) == ']') 
        {
            balance--;

        }
        //Check if it's the right bracket
        if (balance == 1) {
            endPoint = j;
        }
        }

        //Only do such a thing if the cell is not equal to 0
        if (cell[pointer] > 0 && endPoint != -1) 
        {
        codeToRedo = brainfuck.substring(i, endPoint);
        output += brainfuckExecute(codeToRedo);
        } 
        else if (endPoint == -1) //If endpoint is equal to -1, that means that there was no closing bracket
        {
        errorList += "ERROR: No closing bracket (" + i + ")";
        }
    }

对于缩进,我深表歉意,它没有很好地复制 Netbeans。但无论如何,正如您在代码顶部看到的那样,我创建了一个名为 'codeSection,' 的变量,它应该包含正确的文本。这是我为参数 'brainfuck' 提供的内容:+++[>+++++ +++++<-]>+++. 这是我打印变量时得到的内容:

[>+++++ +++++<-]>+++.
[>+++++ +++

是的,我用 System.out.println 打印变量,我打印了两个不同的东西。第一次,它是正确的字符串。第二次我得到处理的内容,没有右括号。我在 brainfuck 的代码中安装了一个错误检查系统,所以我可以检查。我得到了我应该得到的错误。 (错误:没有右括号 (0)) 我真的对这个感到茫然和困惑,所以非常感谢提供的任何帮助。

P.S。额外的代码,比如它被调用的地方等等: 在哪里叫:

String brainfuckExecute(String brainfuck)
    {
        //Reset the output
        output = "";

        //Loop through all instructions
        for(short i = 0; i < brainfuck.length(); i++)
        {
        //Execute a switch to do the instructions
        switch(brainfuck.charAt(i))
        {
            //Increment current cell
            case '+': 
            incrementCell();
            break;
            //Decrement current cell
            case '-': 
            decrementCell(i);
            break;
            //Move pointer up
            case '>': 
            incrementPointer(i);
            break;
            //Move pointer down
            case '<': 
            decrementPointer(i);
            break;
            //Get user input and store it in the current cell
            case ',': 
            getInput();
            break;
            //Add the cell to the output string
            case '.': 
            addOutput();
            break;
            //Start the while loop -- Recurssive
            case '[': 
            openBracket(i, brainfuck);
            break;
        }
        }

        //Return said output
        return output;
    }

(它在具有所有这些变量的 class 中) 其中主要是:

Brainfuck codeTranslator = new Brainfuck();
    System.out.println(codeTranslator.brainfuckExecute("+++[>+++++ +++++<-]>+++."));
    System.out.println(codeTranslator.getErrors());

再次感谢

第一次调用openBracket()时,有i == 3brainfuck == "+++[>+++++ +++++<-]>+++."。因此,在第一个 substring() 调用中,您得到:

String codeSection = brainfuck.substring(3);

这导致:

String codeSection = "+++[>+++++ +++++<-]>+++.".substring(3);
String codeSection = "[>+++++ +++++<-]>+++."

然后你遍历codeSection看它是否平衡。问题是您已经删除了 [ 之前的部分,但是您在 j = 3 处开始循环。我将当前索引位置标记为( ),这样更容易理解:

您在 j = 3:

开始循环
[>+(+)+++ +++++<-]>+++.

一直到 j = 14,这是 -] 之前的索引:

[>+++++ +++++<(-)]>+++.

由于没有找到第一个[,所以balance的值一直是1。所以,最后,你做到了 endPoint = 14.

if (balance == 1) {
    endPoint = j;
}

在下一次迭代中,您会找到 ],并且 balance 会减少到 0,因此循环会继续进行,但 endPoint 不会再次更新。

之后,你做:

codeToRedo = brainfuck.substring(i, endPoint);

这导致:

codeToRedo = brainfuck.substring(3, 14);
codeToRedo = "+++[>+++++ +++++<-]>+++.".substring(3, 14);
codeToRedo = "[>+++++ +++";

我猜你的循环应该是:

for (short j = 0; j < codeSection.length(); j++) { ... }

最后,为了仅获取 brainfuck 中括号之间的部分,您应该注意到,虽然 balance 为 1,但您不断更新 endPoint 的值。我想当你找到平衡支架时你想停下来,所以你可以在你的 if:

中添加一个 break
for (short j = 0; j < codeSection.length(); j++) {
    //Check the character
    if (codeSection.charAt(j) == '[') {
        balance++;
    } else if (codeSection.charAt(j) == ']') {
        balance--;
    }
    //Check if it's the right bracket
    if (balance == 1) {
        endPoint = j;
        break; // Stop when you find the ] position
    }
}

然后做:

codeToRedo = brainfuck.substring(i + 1, i + endPoint);

或者:

codeToRedo = codeSection.substring(1, endPoint);

此外,当您执行 output += brainfuckExecute(codeToRedo) 时,您连接了 brainfuckExecute() 的结果。这可能就是为什么它看起来像是打印了两次。