Read input from console in Java
This post will discuss how to read input from console in Java using Scanner, BufferedReader, and Console class. The input can be of different types, such as String, Integer, Double, Long, etc.
1. Using Scanner Class
The standard approach is to use the Scanner class in Java for reading input from the console, which splits the input into tokens using whitespace as a delimiter, which then can be conveniently converted into values of different types using the various next methods, 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 24 25 26 27 28 29 |
import java.util.Scanner; class Main { // Program to read input from console in Java using `Scanner` public static void main(String[] args) { // Constructs a new `Scanner` instance to scan values from the input stream Scanner scanner = new Scanner(System.in); // read the whole line System.out.println(scanner.nextLine()); // get the next double token System.out.println(scanner.nextDouble()); // get the next string token System.out.println(scanner.next()); // get the next integer token System.out.println(scanner.nextInt()); // get the next long token System.out.println(scanner.nextLong()); // close the scanner scanner.close(); } } |
2. Using BufferedReader Class
The problem with the Scanner class is that it is way too slow. We can also use the BufferedReader class in Java, which offers much better performance than the Scanner class. The whole line can be read as a string using the readLine() method and can be converted into values of different types using the utility methods provided by corresponding wrapper classes. For an integer, we can use the read() method.
|
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 28 29 30 31 |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { // Program to read input from console in Java by using `BufferedReader` public static void main(String[] args) { BufferedReader br = null; try { // Create a buffered character-input stream br = new BufferedReader(new InputStreamReader(System.in)); // read the whole line System.out.println(br.readLine()); // get double System.out.println(Double.parseDouble(br.readLine())); // get integer System.out.println(br.read()); // close the stream br.close(); } catch (IOException e) { e.printStackTrace(); } } } |
3. Using Console Class
We can also use the Console class to access the character-based console device. To get the unique Console object associated with the current JVM, we can call the System.console() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.Console; class Main { // Program to read input from console in Java by using `Console` class // This will only work with the command line environment. public static void main(String[] args) { Console console = System.console(); if (console != null) { // read the whole line System.out.println(console.readLine()); // get double System.out.println(Double.parseDouble(console.readLine())); // get integer System.out.println(Integer.parseInt(console.readLine())); } } } |
That’s all about reading input from the 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 :)