HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / I/O / Java Read Console Input

Java Read Console Input

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 !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Hemant singh

    August 8, 2014

    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

      August 8, 2014

      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

        August 8, 2014

        yeah exactly I was trying to run from My Eclipse……so I tested again from command line ,it is working fine…thanks again

Comments are closed on this article!

Search Tutorials

Java IO

  • Java IO Introduction
  • Java How IO works?
  • Java IO vs NIO
  • Java Create File
  • Java Write to File
  • Java Append to File
  • Java Read File
  • Java Read File to String
  • Java Read File to Byte[]
  • Java Make File Read Only
  • Java Copy File
  • Java Copy Directory
  • Java Delete Directory
  • Java Current Working Directory
  • Java Read/Write Properties File
  • Java Read File from Resources
  • Java Read File from Classpath
  • Java Read/Write UTF-8 Data
  • Java Check if File Exist
  • Java Create Temporary File
  • Java Write to Temporary File
  • Java Delete Temporary File
  • Java Read from Console
  • Java Typesafe input using Scanner
  • Java Password Protected Zip
  • Java Unzip with Subdirectories
  • Java Generate SHA/MD5
  • Java Read CSV File
  • Java InputStream to String
  • Java String to InputStream
  • Java OutputStream to InputStream
  • Java InputStreamReader
  • Java BufferedReader
  • Java FileReader
  • Java LineNumberReader
  • Java StringReader
  • Java FileWriter
  • Java BufferedWriter
  • Java FilenameFilter
  • Java FileFilter

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces