为什么在此代码中出现 "No line found" 异常?
Why do I get a "No line found" Exception in this code?
以下方法会导致 No line found Exception
:
private static String getRebaseAnswer(boolean isFirst, boolean isLast) {
System.out.println("Would you like to (c)ontinue, (s)kip this commit, or"
+ " change this commit's (m)essage?");
Scanner in = new Scanner(System.in);
String answer;
while (true) {
answer = in.nextLine(); // <--- This Line
if (answer.equals("c") || answer.equals("m")) {
in.close();
return answer;
} else if (answer.equals("s") && !isFirst && !isLast) {
in.close();
return answer;
} else {
System.out.println("Would you like to (c)ontinue, (s)kip this commit, or"
+ " change this commit's (m)essage?");
}
}
}
我在这个方法中调用方法:
...
String answer;
Scanner in;
currHead = branchHeads.get(arg);
while (toRebase != null) {
System.out.println("Currently replaying:");
toRebase.getNode().printInfo();
answer = getRebaseAnswer(isFirst, toRebase.getParent() == null); // <--- This Line
...
导致错误的原因是什么??扫描仪不应该等我输入一行再继续getRebaseAnswer
方法吗?我代码中的另一个方法与上述方法具有完全相同的结构,并且没有遇到任何问题。我已经检查了关于这个问题的其他多个帖子,但他们的建议都与这个问题无关或没有解决它。
此方法运行没有问题:
private static boolean handleDangerous() {
System.out.println("Warning: The command you entered may alter the files in your"
+ " working directory. Uncommitted changes may be lost. Are you sure you"
+ " want to continue? (yes/no)");
Scanner in = new Scanner(System.in);
String answer;
while (true) {
answer = in.nextLine();
if (answer.equals("yes")) {
in.close();
return true;
} else if (answer.equals("no")) {
in.close();
return false;
} else {
System.out.println("Not a valid answer, please enter (yes/no).");
}
}
}
当您创建连接到 System.in
的扫描仪并关闭它时,您也会关闭 System.in
。因此,后续尝试读取 System.in
将导致您观察到的异常。
避免这种情况的方法是只创建扫描仪一次,并且在程序完成之前永远不要关闭它。此扫描仪应传递给需要从 System.in
.
读取的任何函数
不要关闭扫描器,否则 Stream 也将被关闭。
in.close();
从当前位置删除这一行并将其放在最后的 main 方法中,这样所有操作流都会关闭..
您可能正在调用其他一些已经关闭流的方法,然后您正在调用此方法。
以下方法会导致 No line found Exception
:
private static String getRebaseAnswer(boolean isFirst, boolean isLast) {
System.out.println("Would you like to (c)ontinue, (s)kip this commit, or"
+ " change this commit's (m)essage?");
Scanner in = new Scanner(System.in);
String answer;
while (true) {
answer = in.nextLine(); // <--- This Line
if (answer.equals("c") || answer.equals("m")) {
in.close();
return answer;
} else if (answer.equals("s") && !isFirst && !isLast) {
in.close();
return answer;
} else {
System.out.println("Would you like to (c)ontinue, (s)kip this commit, or"
+ " change this commit's (m)essage?");
}
}
}
我在这个方法中调用方法:
...
String answer;
Scanner in;
currHead = branchHeads.get(arg);
while (toRebase != null) {
System.out.println("Currently replaying:");
toRebase.getNode().printInfo();
answer = getRebaseAnswer(isFirst, toRebase.getParent() == null); // <--- This Line
...
导致错误的原因是什么??扫描仪不应该等我输入一行再继续getRebaseAnswer
方法吗?我代码中的另一个方法与上述方法具有完全相同的结构,并且没有遇到任何问题。我已经检查了关于这个问题的其他多个帖子,但他们的建议都与这个问题无关或没有解决它。
此方法运行没有问题:
private static boolean handleDangerous() {
System.out.println("Warning: The command you entered may alter the files in your"
+ " working directory. Uncommitted changes may be lost. Are you sure you"
+ " want to continue? (yes/no)");
Scanner in = new Scanner(System.in);
String answer;
while (true) {
answer = in.nextLine();
if (answer.equals("yes")) {
in.close();
return true;
} else if (answer.equals("no")) {
in.close();
return false;
} else {
System.out.println("Not a valid answer, please enter (yes/no).");
}
}
}
当您创建连接到 System.in
的扫描仪并关闭它时,您也会关闭 System.in
。因此,后续尝试读取 System.in
将导致您观察到的异常。
避免这种情况的方法是只创建扫描仪一次,并且在程序完成之前永远不要关闭它。此扫描仪应传递给需要从 System.in
.
不要关闭扫描器,否则 Stream 也将被关闭。
in.close();
从当前位置删除这一行并将其放在最后的 main 方法中,这样所有操作流都会关闭..
您可能正在调用其他一些已经关闭流的方法,然后您正在调用此方法。