Q:

Java Scanner, Delimiters, and End of Line

I have a data file with slash-delimited ints on multiple successive lines.
Here's a little piece of code that I'd like to use to read and print all those ints:
import java.util.Scanner;
import java.io.File;

class Testing {
   public static void main (String[] args) throws Exception {
      Scanner file = new Scanner(new File("data.txt"));
      file.useDelimiter("/");
      while (file.hasNextInt())
         System.out.println(file.nextInt());
   }
}

Data file one looks like this:
1/2/3
4/5/6

Output from data file one looks like this:
1
2

Data file two looks like this:
1/2/3

Output from data file two looks like this:
1
2
3

Can someone explain why I'm getting this behavior?  In particular, why does the first example ignore the presence of the "3" at the end of the first line of data?
Note: I know several "better" ways to get my desired behavior: read the whole line at once and use split() and parseInt(), read the whole line at once and turn it into a separate Scanner object, etc.. That's not my question.  I'm trying to understand why this particular piece of code doesn't work as intended --- in particular, why I get less data when the file has more lines.

A:

The last hasNextInt() will read the element with the carriage return character.  i.e the element will be 3\r\n.  Which is obviously not int, hence it will fail the test file.hasNextInt()

