The hasNextLine method in Scanner check if there is another line in the input of this scanner.
/** * @author lautturi.com * Java example: java Scanner hasNextLine */ import java.io.File; import java.io.FileNotFoundException; import java.util.*; public class Lautturi { public static void main(String[] args) throws FileNotFoundException { // Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(new File("test.txt")); // the tokens are delimited by whitespace by default. System.out.println("Delimiter in Scanner:" + scanner.delimiter()); System.out.println("--first line--:"); // Print the first line System.out.println(scanner.nextLine()); System.out.println("--second line--:"); // Print the second line if (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } System.out.println("--rest lines--:"); // Print the rest lines while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } // Close the scanner scanner.close(); } }
output:
Delimiter in Scanner:\p{javaWhitespace}+ --first line--: lautturi --second line--: hello world --rest lines--: java python perl js Lautturi Java