0

I have a problem in my Java program. I have a while loop which the program completely skips. 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). 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.

I will only write the while loop, since my program is very long. This while loop is inside a method (which only contains this loop). (I use easyIO which is similar to Scanner, only a less complex version)

    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;
    }
6
  • 5
    Post the In class please Commented Oct 9, 2013 at 20:20
  • 9
    if file never hasNext() then the while loop won't run Commented Oct 9, 2013 at 20:20
  • The file is very long, therefore this shouldn't be a problem. Commented Oct 9, 2013 at 20:22
  • 2
    Without seeing the code we can't verify that hasNext() actually works. Hard-to-find bugs are always in code you're 100% absolutely positive can't be the problem. Commented Oct 9, 2013 at 20:24
  • 2
    Java runs in a very particular way which never involves just 'skipping' code. If you have a print statement in your while loop that isn't getting called, there is only one possible culprit. file.hasNext() == false. There's probably some issue in the filename you're using or something Commented Oct 9, 2013 at 20:24

1 Answer 1

2

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? 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.