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:

Download Code

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.

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

Download Code

That’s all about reading input from the console in Java.