CodeGym/Java Blog/Java IO & NIO/Scanner nextLine() Method in Java
Author
Volodymyr Portianko
Java Engineer at Playtika

Scanner nextLine() Method in Java

Published in the Java IO & NIO group
members
Scanner, a class in the Java programming language that can parse primitive types and strings using regular expressions. The nextLine() scanner method in Java moves this scanner beyond the current line and returns the skipped input. More on nextLine() and Scanner later in this article.

Briefly about Scanner class and how it works

Roughly speaking, java.util.Scanner class allows you to read input from various sources, including the console. It really looks similar to a classical scanner. This hardware device has complicated architecture, but it’s pretty simple to describe its work. Scanner reads a date that a user puts on it, such as papers and keeps the data in memory like a picture or pdf file. Java scanner, just like a real one, reads data from the source that you specify for it. For example, from a string, from a file, from a console. Then it recognizes this information and processes it as needed. For example, the program asks to enter some data from the console and reads it or wants to read it from a file. For this operation, the scanner has several methods combined with the word “next”. Such as next(), nextLine(), nextInt(), nextDouble().

nextLine() method

As we’ve said above, the scanner's object splits the input into tokens using the delimiter pattern, whitespace by default, but it can be replaced with a string (java.lang.String) or a regular expression (java.util.regex.Pattern). The resulting tokens can then be converted to values of various types using various next methods. Scanner nextLine() method in Java advances this scanner past the current line and returns the input that was skipped. General next() finds and returns the next complete token from this scanner. nextLine() method continues searching the input for a line separator, so it may buffer all input looking for a line to skip if there are no line separators. This method throws next exceptions:
  • NoSuchElementException: throws if no line was found
  • IllegalStateException: throws if this scanner is closed

nextLine() example

Let's say we want the user to enter any word or phrase into the console, and we could show him what word or phrase he entered.
import java.util.Scanner;
//…scanner.nextLine() example
public class ScannerTest {
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter any phrase and I'll repeat it!... ");
      //here we are reading the next line:
       String sentence = scan.nextLine();
       System.out.println(sentence);
   }
}
The output can be for example the next:
Enter any phrase and I'll repeat it!... here is my phrase here is my phrase
If you use next() method instead of nextLine(), you’ll get just the first word of your sentence.
Enter any phrase and I'll repeat it!... here is my phrase here
Let’s have an example of a working scanner nextLine() method to read from a file. For this example I’ve created a txt file myTest.txt. There are two lines in that file:
my test file and my next line is here
Here is a program that reads two lines from a file.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerTest {

   public static void main(String[] args) throws FileNotFoundException {
       Scanner scanner = new Scanner(new File("d://myTest.txt"));

       String textFromFile1 = scanner.nextLine();
       String textFromFile2 = scanner.nextLine();
       System.out.println(textFromFile1);
       System.out.println(textFromFile2);
       scanner.close();

   }
}
The output is:
my test file and my next line is here
Scanner nextLine() Method in Java - 1If your path to the file is wrong or file with such name doesn’t exists, you’ll get the next output:
Exception in thread "main" java.io.FileNotFoundException: d:\myTest.txt at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:213) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:155) at java.base/java.util.Scanner.<init>(Scanner.java:639) at ScannerTest.main(ScannerTest.java:8)
If the program found your file, but it’s empty, you’ll get NoSuchElementException:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at ScannerTest.main(ScannerTest.java:10)
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet