Read multi-line input from console in Java
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().
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Scanner; import java.util.List; import java.util.ArrayList; class Main { // Read multiline input in Java by using two `Scanner` instances public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { List<String> tokens = new ArrayList<>(); Scanner lineScanner = new Scanner(scanner.nextLine()); while (lineScanner.hasNext()) { tokens.add(lineScanner.next()); } lineScanner.close(); System.out.println(tokens); } scanner.close(); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.Scanner; class Main { // Read multiline input in Java using `Scanner` class public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String[] tokens = scanner.nextLine().split("\\s"); System.out.println(Arrays.toString(tokens)); } scanner.close(); } } |
Here’s another version where only Scanner.next() is used:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Main { // Read multiline input in Java using `Scanner` class public static void main(String[] args) { List<String> tokens = new ArrayList<>(); Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { tokens.add(scanner.next()); } scanner.close(); System.out.println(tokens); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; class Main { // Read multiline input in Java using `BufferedReader` class public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; try { while ((line = br.readLine()) != null) { String[] tokens = line.split("\\s"); System.out.println(Arrays.toString(tokens)); } br.close(); } catch (IOException e) { e.printStackTrace(); } } } |
From Java 7 onward, we should use try-with-resources, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; class Main { // Read multiline input in Java 7 using `BufferedReader` class public static void main(String[] args) { try (InputStreamReader in = new InputStreamReader(System.in); BufferedReader buffer = new BufferedReader(in)) { String line; while ((line = buffer.readLine()) != null) { String[] tokens = line.split("\\s"); System.out.println(Arrays.toString(tokens)); } } catch (Exception e) { e.printStackTrace(); } } } |
That’s all about reading multi-line Input from Console in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)