Home Java Scanner Methods hasNextLine() Java Scanner hasNextLine() Method Definition In Java, sc.hasNextLine() returns true if there is another line in the scanner's input source—even if that line is empty ("").
It is the standard guard for while (sc.hasNextLine()) loops so calling nextLine() never throws NoSuchElementException.
An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.hasNextLine(...).
Syntax Wrap nextLine() in a hasNextLine() loop to read every line safely until EOF.
Scanner sc = new Scanner("one\ntwo\n");
while (sc.hasNextLine()) {
String line = sc.nextLine();
} Call What it does sc.hasNextLine()Returns true while any unread line remains. sc.nextLine()Reads and consumes that next line.
A single trailing newline at the very end of input (for example "one\ntwo\n") ends line 2; it does not create a third empty line, so hasNextLine() returns false after "two".
Library declarations // java.util.Scanner
public boolean hasNextLine() Parameters Parameter Description None hasNextLine() takes no parameters.
Return Value Return Value Description booleantrue if another line (possibly empty) can be read; false at end of input.Throws IllegalStateException If the scanner has been closed.
Important
Two consecutive line breaks like "a\n\nb" create a real blank middle line where hasNextLine() is true and nextLine() returns "", whereas a single trailing newline at EOF does not. Note also that calling nextInt() before hasNextLine() leaves the current line's newline unread, so hasNextLine() stays true for that empty remainder.
Compare with Similar Methods Line lookahead versus token lookahead:
Method How it differs nextLine()Consumes and returns the line checked by hasNextLine(). hasNext()Returns false if only blank lines or whitespace remain. Files.lines()Streams lines directly from a file Path.
Java Examples 1. Trailing newline rule 2. Empty vs whitespace-only 3. Check after nextInt() Example 1 — Why a Trailing Newline Does Not Add a Phantom Empty Line Compare "one\ntwo\n" against "one\n\ntwo" to see exactly when hasNextLine() is true.
import java.util.Scanner;
public class ScannerHasNextLineTrailing {
public static void main(String[] args) {
try (Scanner sc = new Scanner("one\ntwo\n")) {
int n = 0;
while (sc.hasNextLine()) {
System.out.println("Trailing test line " + (++n) + ": [" + sc.nextLine() + "]");
}
System.out.println("Total lines: " + n);
}
try (Scanner sc2 = new Scanner("one\n\ntwo")) {
int n2 = 0;
while (sc2.hasNextLine()) {
System.out.println("Blank test line " + (++n2) + ": [" + sc2.nextLine() + "]");
}
}
}
} Trailing test line 1: [one]
Trailing test line 2: [two]
Total lines: 2
Blank test line 1: [one]
Blank test line 2: []
Blank test line 3: [two] How It Works End newline belongs to line 2. In "one\ntwo\n", nextLine() consumes "two" plus its trailing \n, leaving hasNextLine() false afterwards.Double break creates line 2 empty. In "one\n\ntwo", the first \n ends "one" and the second \n terminates an empty line 2.Bracket formatting reveals blanks. Printing inside [...] makes empty strings visibly distinct from no line.Example 2 — Compare hasNextLine() and hasNext() on Blank Input See why hasNextLine() returns true on spaces-only input while hasNext() returns false.
import java.util.Scanner;
public class ScannerHasNextLineBlank {
public static void main(String[] args) {
try (Scanner empty = new Scanner("");
Scanner spaces = new Scanner(" ")) {
System.out.println("Empty string -> hasNextLine: " + empty.hasNextLine() + ", hasNext: " + empty.hasNext());
System.out.println("Spaces only -> hasNextLine: " + spaces.hasNextLine() + ", hasNext: " + spaces.hasNext());
}
}
} Empty string -> hasNextLine: false, hasNext: false
Spaces only -> hasNextLine: true, hasNext: false How It Works Zero characters means no line. On "", neither a line nor a token exists, so both methods print false.Spaces count as a line. Three spaces form a valid line for hasNextLine(), returning true.Whitespace-only has no tokens. hasNext() skips whitespace delimiters and finds nothing left, so it returns false.Example 3 — See hasNextLine() After Reading a Trailing-Newline Integer Check hasNextLine() right after nextInt() on "42\nDone" to observe the leftover line break.
import java.util.Scanner;
public class ScannerHasNextLineAfterInt {
public static void main(String[] args) {
try (Scanner sc = new Scanner("42\nDone")) {
int val = sc.nextInt();
System.out.println("Read int: " + val);
System.out.println("hasNextLine right after nextInt: " + sc.hasNextLine());
System.out.println("Remainder of line 1: [" + sc.nextLine() + "]");
System.out.println("Line 2: [" + sc.nextLine() + "]");
System.out.println("hasNextLine at EOF: " + sc.hasNextLine());
}
}
} Read int: 42
hasNextLine right after nextInt: true
Remainder of line 1: []
Line 2: [Done]
hasNextLine at EOF: false How It Works Cursor remains on line 1. Reading 42 stops before \n, so hasNextLine() sees the rest of line 1 waiting.Empty remainder cleared. The first nextLine() consumes \n and prints [].Final line finishes cleanly. After reading "Done", hasNextLine() becomes false.Frequently Asked Questions Does a trailing newline at the end of a file create an extra empty line for hasNextLine()? No. "a\nb\n" has two lines ("a" and "b"), and hasNextLine() returns false after "b".
What does hasNextLine() return on an empty string ""? It returns false because there are zero characters and zero lines.
Why does hasNextLine() return true on a blank line? A blank line containing spaces or an empty line before another newline still counts as a line for nextLine().
Does hasNextLine() block when reading System.in? Yes. If no characters or newline are currently available from the keyboard, hasNextLine() waits until you type a line or end the stream.
What exception does hasNextLine() throw if the scanner is closed? IllegalStateException.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
10 people found this page helpful
Helpful Share Copy link Suggestion