This post will discuss how to read multiline input from the console using Scanner and BufferedReader class in Java.

1. Using Two Scanners

The idea is to use two scanners – one to get each line using Scanner.nextLine(), and the other to scan through it using Scanner.next().

Download Code

2. Using Single Scanner

We can even read each line with a single scanner. The idea is to read each line using Scanner.nextLine() method and use String.split() or StringTokenizer to read tokens from each line.

Download Code

 
Here’s another version where only Scanner.next() is used:

Download Code

3. Using BufferedReader Class

Another way to read multiple lines from the console can be done using the synchronized BufferedReader class in Java. The idea is to read each line using the readLine() method and use String.split() to split the line into individual tokens using whitespace as a delimiter. We can also use the StringTokenizer class.

Download Code

From Java 7 onward, we should use try-with-resources, as shown below:

Download Code

That’s all about reading multi-line Input from Console in Java.