Read Input from console in Java

In this post, we will see 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 the Scanner class

The standard approach is to use Scanner class in Java for reading an input from 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:


Download   Run Code


2. Using the BufferedReader class

The problem with Scanner class is that it is way too slow. We can also use BufferedReader class in Java which offers much better performance than the Scanner class. The whole line can be read as String using readLine() method, and can be converted into values of different types using the utilites methods provided by corresponding wrapper classes. For an integer, we can use read() method.


Download   Run Code


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 System.console() method.



1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of