In Java, there are three different ways for reading input from the user in the command line environment(console).
1.Using Buffered Reader Class
This is the Java classical method to take input, Introduced in JDK1.0. This method is used by wrapping the System.in (standard input stream) in an InputStreamReader which is wrapped in a BufferedReader, we can read input from the user in the command line.
- The input is buffered for efficient reading.
- The wrapping code is hard to remember.
Note: To read other types, we use functions like Integer.parseInt(), Double.parseDouble(). To read multiple values, we use split().
2. Using Scanner Class
This is probably the most preferred method to take input. The main purpose of the Scanner class is to parse primitive types and strings using regular expressions, however it is also can be used to read input from the user in the command line.
- Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the tokenized input.
- Regular expressions can be used to find tokens.
- The reading methods are not synchronized
To see more differences, please see this article.
GeeksforGeeks 12 3.4
You entered string GeeksforGeeks You entered integer 12 You entered float 3.4
3. Using Console Class
It has been becoming a preferred way for reading user’s input from the command line. In addition, it can be used for reading password-like input without echoing the characters entered by the user; the format string syntax can also be used (like System.out.printf()).
- Reading password without echoing the entered characters.
- Reading methods are synchronized.
- Format string syntax can be used.
- Does not work in non-interactive environment (such as in an IDE).
Please refer this for more faster ways of reading input.
This article is contributed by D Raj Ranu. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- How to read user or console input in PHP ?
- Taking input from console in Python
- Java.io.Console class in Java
- Console printf(String, Object) method in Java with Examples
- Console readPassword() method in Java with Examples
- Console flush() method in Java with Examples
- Console reader() method in Java with Examples
- Console format(String, Object) method in Java with Examples
- Console writer() method in Java with Examples
- Console readLine() method in Java with Examples
- Java Program to Write an Array of Strings to the Output Console
- Image Processing in Java | Set 1 (Read and Write)
- Java program to read all mobile numbers present in given file
- How to make an ArrayList read only in Java
- How to Read and Print an Integer value in Java
- Reader read(char) method in Java with Examples
- Reader read(char, int, int) method in Java with Examples
- Reader read(CharBuffer) method in Java with Examples
- Reader read() method in Java with Examples
- CharArrayReader read() method in Java with Examples
Improved By : sanjayrajputcse