The method Scanner#hasNextLine()
just tells you if there is one more line available. If you call it twice, it will still just tell you, if there is ONE more line available. You can call it n
times, but it will just tell you about the next line.
Call Scanner#nextLine()
and then you can test again if there is another line available.
Always do it that way:
- Test if more data is available
- Read data
- Test if more data is available
- ...
if (tableLineScanner.hasNextLine()) {
String firstLine = tableLineScanner.nextLine();
if (tableLineScanner.hasNextLine()) {
String secondLine = tableLineScanner.nextLine().replaceAll(...);
tableFields.add(firstLine + secondLine);
} else {
// No-second-line error handling goes here
}
} else {
// No-first-line error handling goes here (if any)
}