Java Utility Library

Java Scanner - hasNextLine() Method



The java.util.Scanner.hasNextLine() method returns true if there is another line in the input of the scanner. This method may block while waiting for input. The scanner does not advance past any input.

Syntax

public boolean hasNextLine()

Parameters

No parameter is required.

Return Value

Returns true if there is another line in the input of the scanner.

Exception

Throws IllegalStateException, if the scanner is closed.

Example:

In the example below, the java.util.Scanner.hasNextLine() method returns the content of the scanner line by line.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {

    //String to scan
    String MyString = "Hello \nWorld \nLearn Programming.";

    //creating a Scanner
    Scanner MyScan = new Scanner(MyString);

    while(MyScan.hasNextLine()) {
      //print the line while there is a nextLine token
      System.out.println(MyScan.nextLine());
    }

    //close the scanner
    MyScan.close();
  }
}

The output of the above code will be:

Hello 
World 
Learn Programming.

❮ Java.util - Scanner