While循环无法退出Java。

9
我输入了一个很大的数字,并将每行数字添加到字符串“hold”中。一旦没有要读取的内容,它就应该退出while循环并将字符串转换为BigInteger。
它会一直运行循环,直到没有剩余的内容,然后什么也不会发生。它不会继续转换为BigInteger,也不会再次进入循环。我已经到处看了,也无法弄清楚它为什么卡住了。
System.out.print("Enter the number you want looked at: ");
String hold = "";
String line = null;
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
while ((line = read.readLine()) != null){
    hold = hold.concat(line);
}
BigInteger giantNumber = new BigInteger(hold);

System.out.print(giantNumber);

我希望它在用户仅输入数字后退出循环。

似乎它被卡住了,因为System.in正在寻找更多的输入,并且在while循环中没有接收到任何要比较的内容。从那里开始,我不知道该怎么做。

如果有帮助的话,我输入的数字是:

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450


1
如果你只读取单行,为什么需要 while 循环? - jmj
@JigarJoshi 这个数字分布在几行中。123 换行 456 等等。这样说清楚了吗? - BarbarianBunny
@CommuSoft 我正在使用Intellij的调试菜单,如果你是指那个的话。 - BarbarianBunny
2
如果您想通过输入回车来退出,则可能需要检查空字符串而不是null。 - Tony Vu
@JigarJoshi 用户一次输入整个带有换行符的数字,因此用户不需要再输入终止符。 - BarbarianBunny
显示剩余9条评论
4个回答

6

System.in会一直读取直到数据流结束。如果您在Java程序的交互模式下使用它,这意味着该字符串通常不会结束,除非用户按下Ctrl+D或终端操作员识别的其他关闭输入流的键组合。

您可以采取以下几种方式:

  • use Ctrl+D to end the terminal input

  • Use a file as input feed (java -jar program.jar < inputfile). In that case the stream will terminate after the end of the file

  • implement a stop code:

    while ((line = read.readLine()) != null){
        if(line.equals("stop")) {
            break;
        }
        hold = hold.concat(line);
    }
    

    Here the java program will end when you enter stop for a certain line.


有没有办法让它在没有输入剩余内容时停止,例如 ((line = ...) != "") 或其他什么方法? - BarbarianBunny

1
如果您想在空行处插入换行符,请获取该行的修剪版本并检查是否为空。
您的方法:
    while ((line = read.readLine()) != null){
        if(line.trim().equals("")){
            break;
        }
        hold = hold.concat(line);
    }

使用Scanner代替:
...
    Scanner in = new Scanner(System.in);
    while(in.hasNextLine()){
        String line = in.nextLine();
        if(line.trim().equals("")){
            break;
        }
        hold = hold.concat(line);
...

它无法解析in。那应该是System.in吗? - BarbarianBunny
这两个都完美运行。你建议一般使用Scanner还是BufferedReader,还是要视情况而定? - BarbarianBunny
1
在这种情况下,使用Scanner更好,因为Scanner具有一些内置功能,如hasNextLine()nextInt等。BufferedReader也有其自己的用例,并且可以与Scanner一起使用。https://dev59.com/j3E95IYBdhLWcg3wn_Rr - Nitin Dandriyal

0

在这种情况下,在条件中进行赋值是可以的,因为该赋值被额外的一对括号所包围 - 比较显然是!= null,我们不可能想要输入 line == reader.readLine()

但是,在这里使用for循环可能会更加优雅:

for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    doSomething(line);
}

Alternatively, we could do this which also restricts the scope of line as with the for-loop, and additionally eliminates unnecessary repetition: Note: This method doesn't mutate any variables

while (true) {
    final String line = reader.readLine();
    if (line == null) break;

    doSomething(line);
}


0
你可以使用字符串类的isEmpty()函数来检查字符串是否为空。
while ((line = read.readLine()) != null&&!line.isEmpty())
{
     hold = hold.concat(line);
}

System.out.print(hold);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接