简体   繁体   English

程序跳过while循环-没有错误

[英]Program skips while-loop - no error

I have a problem in my Java program. 我的Java程序有问题。 I have a while loop which the program completely skips. 我有一个while循环,程序完全跳过了这个循环。 I am sure that it skips the while-loop, as I have tested by trying to print out a sentence inside the while loop, which is not executed (neither is the rest of the loop). 我确定它会跳过while循环,因为我已经尝试通过在while循环内打印出一个语句来测试该语句,该语句不会执行(循环的其余部分也不执行)。 I don't get an error when i compile or run the program, and I really can't see what's wrong. 编译或运行程序时我没有收到错误,而且我真的看不到有什么问题。 If anybody could help me, that would be great. 如果有人可以帮助我,那就太好了。 I have a while loop extremely similar to this which runs as expected, therefore I find it strange that this one doesn't work. 我有一个while循环,该循环与按预期运行的循环极为相似,因此我感到奇怪的是,这个循环不起作用。

I will only write the while loop, since my program is very long. 由于我的程序很长,因此我只会编写while循环。 This while loop is inside a method (which only contains this loop). 此while循环位于方法内部(仅包含此循环)。 (I use easyIO which is similar to Scanner, only a less complex version) (我使用的是easyIO,它类似于Scanner,只是不太复杂的版本)

    int i = 0;
    In file = (infile); //Infile is declared earlier in the program 
    int [][] array2D = new int [unique][unique];

    String word1 = file.inWord();

    while (file.hasNext()) {
        String word2 = file.next();
        word1 = word2;
    }

Since you say "infile" is declared earlier in your code, is it possible that you've already iterated through the file earlier in your code? 由于您说“ infile”已在代码中更早声明,是否有可能已经在代码中更早地遍历了文件? If that's the case, since you're assigning "file" the same reference as "infile" if the pointer is already at the end of the file file.hasNext() will obviously evaluate to false. 如果是这种情况,因为如果指针已经位于文件file的末尾, file.hasNext()您分配“ file”的引用与“ infile”相同file.hasNext()显然将为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM