Java Scanner hasNextLine() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Scanner

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.

Java
Scanner sc = new Scanner("one\ntwo\n");
while (sc.hasNextLine()) {
    String line = sc.nextLine();
}
CallWhat 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
// java.util.Scanner
public boolean hasNextLine()

Parameters

ParameterDescription
NonehasNextLine() takes no parameters.

Return Value

Return ValueDescription
booleantrue if another line (possibly empty) can be read; false at end of input.
Throws IllegalStateExceptionIf the scanner has been closed.

Compare with Similar Methods

Line lookahead versus token lookahead:

MethodHow 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

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.

Java
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() + "]");
            }
        }
    }
}

How It Works

  1. End newline belongs to line 2. In "one\ntwo\n", nextLine() consumes "two" plus its trailing \n, leaving hasNextLine() false afterwards.
  2. Double break creates line 2 empty. In "one\n\ntwo", the first \n ends "one" and the second \n terminates an empty line 2.
  3. 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.

Java
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());
        }
    }
}

How It Works

  1. Zero characters means no line. On "", neither a line nor a token exists, so both methods print false.
  2. Spaces count as a line. Three spaces form a valid line for hasNextLine(), returning true.
  3. 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.

Java
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());
        }
    }
}

How It Works

  1. Cursor remains on line 1. Reading 42 stops before \n, so hasNextLine() sees the rest of line 1 waiting.
  2. Empty remainder cleared. The first nextLine() consumes \n and prints [].
  3. Final line finishes cleanly. After reading "Done", hasNextLine() becomes false.

Frequently Asked Questions

No. "a\nb\n" has two lines ("a" and "b"), and hasNextLine() returns false after "b".
It returns false because there are zero characters and zero lines.
A blank line containing spaces or an empty line before another newline still counts as a line for nextLine().
Yes. If no characters or newline are currently available from the keyboard, hasNextLine() waits until you type a line or end the stream.
IllegalStateException.

Next: hasNextLong()

Check whether the next token fits in a signed long.

hasNextLong() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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