-1

Consider the following java code which opens a file, reads all of the data, then tries to read one more line.

public static void main ( String[] argv) {

    FileInputStream inFile = null;
    try {
        inFile = new FileInputStream("DemoRead.txt");
    }
    catch ( FileNotFoundException ex) {
        System.out.println("Could not open file "+ ex.getMessage());
        System.exit(0);
    }

    Scanner inputStream = new Scanner( inFile);
    while ( inputStream.hasNext()) {
        System.out.println( inputStream.nextLine());
    }
    System.out.println(inputStream.nextLine());

    inputStream.close();
}

I would expect that the final inputStream.nextLine() would throw an exception as there is nothing else to read.

Indeed if I change the while loop to:

while ( true) {
    System.out.println ( inputStream.nextLine());
}

It does throw an exception as expected.

This is not making any sense to me. Any insight would be appreciated.

  • What's the last thing printed out, in the first case? – Edward Peters Aug 29 '16 at 17:26
  • 3
    Using your code, I get a `NoSuchElementException` as expected. Please double-check your code. – Seelenvirtuose Aug 29 '16 at 17:27
  • 3
    Further, you probably want to check the condition: `hasNextLine()` in the while loop instead of `hasNext()` which looks for the next token. – Nir Alfasi Aug 29 '16 at 17:28
  • 1
    Are there spaces or new lines at the end of the file? – fgb Aug 29 '16 at 17:31
  • Possible duplicate of [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – Tom Aug 29 '16 at 17:57
  • @Seelenvirtuose The exception is only expected for files, which contravenes against POSIX conventions (i.e. don't have a line ending on each line) – Tom Aug 29 '16 at 18:01

2 Answers2

2

hasNext() can return false before the end of the file if there are no more tokens left. This is the case when there are only delimiters left at the end of the file.

Compare this with hasNextLine(). If you're using nextLine() then the condition should be hasNextLine(). hasNext() is used with next().

fgb
  • 18,439
  • 2
  • 38
  • 52
0

1stly hasNext()--Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.But in case of while loop we always get true.

2ndly in case of nextLine()--Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

so,in case of while(true) we are getting always true and at certain point the nextLine() method finds no line separator so gives exception, but in case of while(hasNext()) method it returns false when there is no token in the file so loop breaks so no Exception comes.. Hope this may help you out.

Rakesh shah
  • 131
  • 1
  • 4