In this Java tutorial, we will learn how to read input from the console in Java. Reading console input in programs may be necessary to make applications interactive.
Using the console input, we can ask the users to provide the input to the application which application will process, and then will print output to the console.
Table of contents 1. java.io.Console class 2. BufferedReader class 3. Scanner class
1. Read console input with Console class
Console
class is from code package and is used to read from and write to the console.System.console()
is used to get the reference of system console.- The
readLine()
method writes a line to the console and then read a line from the console. - The
readPassword()
method is used to read the secure input. For example, passwords and encryption keys.
Console console = System.console(); String inputString = console.readLine("Enter Your Name: "); System.out.println("The name entered: " + inputString);
The program output:
Enter Your Name: Lokesh The name entered: Lokesh
2. Read console input using BufferedReader
BufferedReader
is supported since Java 1.1. We may see its usage in legacy Java application.
To read console input, we shall wrap the System.in
(standard input stream) in an InputStreamReader
which again wrapped in a BufferedReader
class.
BufferedReader
reads text from the console, buffering characters so as to provide for the efficient reading of user input. It makes the read operations from InputStreamReader
– less costly.
System.out.print("Enter Your Name: "); BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in)); String inputString = bufferRead.readLine(); System.out.println("The name entered: " + inputString);
The program output:
Enter Your Name: Lokesh The name entered: Lokesh
3. Java Read Console Input using Scanner
In Java, System.in represents the stanadard input. By deafult, it is system console.
The Scanner class, when reading from the console, provides methods to read different types of data e.g. integers, numbers, strings, etc.
Scanner scanner = new Scanner(System.in); System.out.println("Enter name, age and salary:"); String name = scanner.nextLine(); int age = scanner.nextInt(); double salary = scanner.nextDouble(); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Salary: " + salary);
Above all techniques are equally effective, but I personally like the java.io.Console
way. It simply makes code more readable. What is your choice to read the test from Console in Java.
Happy Learning !!
Hemant singh
Hi Lokesh,Thanks for sharing this post.
for me usingConsoleReader() function not working.
Console console=null;
console = System.console();
System.out.println(console); //always printing null for me. so further part not executing.
can you tell me what will be the cause here?
rest two function working properly.
Lokesh Gupta
The evaluation of System.console() depends entirely on the environment you run application in it. As per my knowledge, both Eclipse and Netbeans will cause System.console() to return null whereas running your application on the same machine’s command line shall return a valid Console object.
Hemant singh
yeah exactly I was trying to run from My Eclipse……so I tested again from command line ,it is working fine…thanks again