0

Why is this loop not printing the last line in the .txt file that I have saved elsewhere? It prints out all the lines except the very last one.

int count = 0;

Courses[] POS = new Courses[26];

while (scan.hasNext())
{
    POS[count] = new Courses(scan.nextLine());

    System.out.println(POS[count]);
    scan.nextLine();

    count++;
}
1
  • 3
    Do you mean to do nextLine() twice? Commented Jan 30, 2013 at 19:51

2 Answers 2

1

You are getting the next line twice. You are lucky it doesn't throw a NPE or another exception :P

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

Comments

0

I think the problem is that, as demongolem pointed out in the comments, you're calling scan.nextLine() twice.

If you have an even-numbered length of lines, that would mean this would print out every other line. If you have an odd-numbered length of lines, that would mean it would print out every other line, then throw an exception (NoSuchElement I believe?)

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextLine.

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.