数组省略代码的一部分中的行,但在另一部分中显示

时间:2014-11-06 09:52:18

标签: java arrays

所以我正在创建一个String [],在这部分代码中我想念我正在检查的代码行! *请注意:我知道这不是最好的检查方法,但这只是一个“概念证明”,稍后会有所改变。无论如何,这是给我问题的代码:

private void checkMethods() {

    for (int i  = 0; i < array.length; i++) {
        String s  = array[i];
        System.out.println(s);

        if(array[i].contains("onEnable()")) {
            System.out.println("enable");
            array[i] = MethodUpdate.onEnable;
        } 

        else if (isOnDisable(s)) {
            System.out.println("disable");
            array[i] = MethodUpdate.onDisable;
        } 

        else if (isOverride(s)) {
            if (checkNextLine(array[i++])) {
                array[i] = "";
            }
        }
    }
}

现在返回以下内容:

public class DummyBukkitClass extends JavaPlugin {

  @Override
    // Some Profound code can be found here
  }

  @Override
    // The Profound code is now ending =(
  }

}

但是在下面的代码中它还返回了我需要的内容:

   public void updateFile(File file) throws IOException {
    BufferedReader br = null;
    pw = null;

    try {
        br = new BufferedReader(new FileReader("classes/DummyBukkitClass.java"));
        file.getParentFile().mkdirs();
        pw = new PrintWriter(file);

        String line;
        int index = 0;

        while ((line = br.readLine()) != null) {
            array[index] = line;
            index++;
        }


    } finally {
        checkMethods();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
            pw.println(array[i]);
        }

        br.close();
        pw.flush();
        pw.close();
    }
}

现在返回:

public class DummyBukkitClass extends JavaPlugin {

  @Override
  public void onEnable() {
    // Some Profound code can be found here
  }

  @Override
  public void onDisable() {
    // The Profound code is now ending =(
  }

}

我很难过为什么第一个省略了2行,而我们在那里之后就称它为正!它们可以在整个添加过程中进行打印;我可以在调用checkMethods()之前打印它们。请帮忙!

谢谢!

编辑:

要解决我需要改变的问题:

        else if (isOverride(array[i])) {
            if (checkNextLine(array[i++])) {
                array[i] = "";
            }
        }

在checkMethods()中:

        else if (isOverride(array[i])) {
            if (checkNextLine(array[i+1])) { //HERE
                array[i] = "";
            }
        }

背后的原因是:我增加了'i'的大小然后再使用'i'而不重置它。 'i + 1'修复了因为你没有为i设置新值。

2 个答案:

答案 0 :(得分:1)

将checkNextLine(array [i ++])更改为checkNextLine(array [i + 1]),否则它将跳过行。

答案 1 :(得分:1)

您在第一个样本中递增计数器i而不输出该行:

   else if (isOverride(s)) {
        if (checkNextLine(array[i++])) {
            array[i] = "";
        }
    }