所以我正在创建一个 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 =(
}
}
我只是很困惑为什么第一行省略了两行,而我在它们出现后立即调用它!它们能够在整个添加过程中被打印;我可以在调用 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 设置新值。
最佳答案
将 checkNextLine(array[i++]) 更改为 checkNextLine(array[i + 1]),否则会跳行。
关于java - 数组在代码的一部分中省略行,但在另一部分中显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26776312/