gpt4 book ai didi

java - Eclipse 中的 java.util.Scanner 无限循环

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:24 25 4
gpt4 key购买 nike

我必须从控制台读取这种输入:

word1
word2

word3
word4

我已经尝试过了:

public static void lectSC(Scanner sc, String l, boolean next) {
l=sc.nextLine(); //keep the first line
while(!l.isEmpty()){ //if it is not empty go! until the white line
checkDicc(l);//check if is the word that I am looking for
l=sc.nextLine(); //get the next word
}
l=sc.nextLine(); //take the nextLine which has a word3
while(!l.isEmpty()){
parejas.add(l);
next=sc.hasNext(); //HERE IT IS THE INFINITY LOOP
if(next){ //I try with this next know which is the value of hasNext(), but I never arrive here when it is the last line.
l=sc.nextLine();
}else{
l=""; //It is how it should exit
}
}
System.out.println("OUT");
}

我不得不说,当我从键盘上单击 Ctrl-Z 时,程序会继续,但它不能那样工作。

谢谢!!

最佳答案

The hasNext() method is getting blocked for input

,当从控制台读取时,程序需要一个流,并且它将等待进一步的输入。

下面是该方法的官方 java 文档,它确实提到了它的阻塞性质,但没有太多细节:- http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNext()

关于java - Eclipse 中的 java.util.Scanner 无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41360777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com