Java Console Class – Ways to read Java Console Input

There are many situations when we need to take the input from the user according to his/her requirement. In such cases we can’t take the inputs in our program rather, we have to take input from the console at the execution of the program.

Java provides different ways to take input and provide output on the console. In this article, we will learn how to take console input from the user using java console.

We will learn each way to use a console for user input and output in Java with the help of examples.

So, let’s start exploring different ways to read input from the Java console.

Java Console

There are three ways to take input from console in Java

  1. Using Java BufferedReader class
  2. Through Java Scanner class
  3. Using Java Console class

1. Using BufferedReader class

Using BufferedReader class is the classical method of taking input from the console. Java has introduced this technique since Java 1. The BufferedClass reads data line by line through the readLine() method. This class wraps the System.in with an InputStreamReader.

To use these classes, we need to use the java.io package. Let’s see an example to understand this method of reading input from the console.

Code to read input using the BufferedReader class:

package com.techvidvan.consoleinput;
 
//Java program to demonstrate BufferedReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderDemo
{
    public static void main(String[] args) throws IOException 
    {
        InputStreamReader input = new InputStreamReader(System.in); 
        //Taking the input data using the BufferedReader class
        BufferedReader reader = new BufferedReader(input); 

        // Reading data using readLine
        System.out.println("Please enter the input:");
        String name = reader.readLine();
    
        // Printing the read line
        System.out.println("You entered: ");
        System.out.println(name);        
    }
}

Output:

Please enter the input:
TechVidvan’s Java Tutorial
You entered:
TechVidvan’s Java Tutorial
Using BufferedReader class until the user writes “over”

We can also use the BufferedReader class to take the input from the user until the user types any string that matches with our test String. Let’s take an example to understand this.

Code to understand this example:

package com.techvidvan.consoleinput;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderDemo
{
    public static void main(String[ ] args) throws IOException 
    {
        InputStreamReader input = new InputStreamReader(System.in); 
        //Taking the input data using the BufferedReader class
        BufferedReader reader = new BufferedReader(input); 


        String name = ""; 
        System.out.println("Enter the names and when you want to stop entering the name, type Over");

        while(!name.equals("Over"))
        { 

            System.out.println("Enter the name: "); 
            // Reading data using readLine() method
            name = reader.readLine(); 
          
            // Printing the read line
            System.out.println("The entered name is: "+name); 
           
            if(name.contentEquals("Over"))
                System.out.println("You entered Over!!");
        } 
        reader.close(); 
        input.close(); 
    }  
}

Output:

Enter the names and when you want to stop entering the name, type Over
Enter the name:
Avina
The entered name is: Avina
Enter the name:
Neha
The entered name is: Neha
Enter the name:
Ravi
The entered name is: Ravi
Enter the name:
Over
The entered name is: Over
You entered Over!!

Note: The method readLine() is used to take the String input from the user. If you want to read the other types, we can use the methods like

  • Integer.parseInt(): to read int values
  • Double.parseDouble(): to read double values
  • Float.parseFloat(): to read float values

To read multiple values, we use the split() method.

2. Using Scanner class

The second way to take input from the user is using the java.util.Scanner class. It is probably the best choice of taking console input from the user. This class reads the input from the user in the console or the command line.

The other use of Scanner class is to parse the strings and primitive types with the help of java regular expressions.

The  Scanner class is present in the java.util package. This class obtains the input from System.in (standard input stream).

Secondly, we also use this Console class to read password-like input without displaying the characters of the input password on the console.

Syntax of using Scanner class:

Scanner scanner = new Scanner(System.in);
Advantages
  • The Scanner class provides useful methods like  nextInt(), nextDouble(), nextFloat(), etc, for parsing primitive data types.
  • The Scanner class uses regular expressions and we can use these regular expressions to find tokens.
Drawback
  • The methods of Scanner class for reading values are not synchronized
Input Types of the Scanner class

To read the values of various data types, the Scanner class provides several methods. The following table shows these methods:

Method Description
nextBoolean() This method reads a boolean value from the user
nextByte() This method reads a byte value from the user
nextDouble() This method reads a double value from the user
nextFloat() This method reads a float value from the user
nextInt() This method reads an int value from the user
nextLine() This method reads a String value from the user
nextLong() This method reads a long value from the user
nextShort() This method reads a short value from the user

Code to take input from the user using the Scanner class:

package com.techvidvan.consoleinput;

import java.util.Scanner;
public class ScannerClassDemo
{
    // Java program to understand the use of Scanner in Java
    public static void main(String args[])
    {
        // Using Scanner for Getting Input from User
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a string");
        String string = sc.nextLine();
        System.out.println("You entered string: " +string);

        System.out.println("Enter a number");
        int num = sc.nextInt();
        System.out.println("You entered integer: " +num);

        System.out.println("Enter a float number");
        float fnum = sc.nextFloat();
        System.out.println("You entered float: " +fnum);
    }
}

Output:

Enter a string
TechVidvan
You entered string: TechVidvan
Enter a number
5
You entered integer: 5
Enter a float number
87.4
You entered float: 87.4

3. Using the Java Console class

The Java Console class is the third technique to take input from the user through the console. The Console class was introduced since Java 1.5. This class is present in the java.io package.

There are several methods in Console class that help in reading input texts and passwords from the console, without displaying it on the screen. Secondly, we also use this Console class to read password-like input without displaying the characters of the input password on the console.

Advantages
  • Reading password without displaying the characters of the password on the console.
  • Reading methods of the console class are synchronized.
  • We can also use the format string syntax with the Console class
Drawback
  • It does not work in a non-interactive environment (such as in an IDE).

Methods of Java Console class

The following table shows various methods of Java Console class:

Method Description
Reader reader() This method gets the object of the Reader class related to the console.
String readLine() This method reads a single line of text from the console.
String readLine(String fmt, Object… args) This method provides a formatted prompt before reading the input text from the console.
char[ ] readPassword() It is used to read a password that is visible on the console screen.
char[ ] readPassword(String fmt, Object… args) This method provides a formatted prompt before reading the password that is not visible on the console screen.
Console format(String fmt, Object… args) This method displays a formatted string to the console output stream.
Console printf(String format, Object… args) This method prints a string to the console output stream.
PrintWriter writer() This method is used to get the object of the PrintWriter class.
void flush() This method is used to flush the console.

Code to take input from user using the Console class:

package com.techvidvan.consoleinput;

public class Sample 
{
    public static void main(String[] args) 
    {        
        // Using Console to input data from user
        System.out.println("Enter something");
        String name = System.console().readLine();
        System.out.println("You entered: " +name);
    }
}

Output:

Java console class example

Java Console Example to read password

import java.io.Console; 
class Sample
{   
     public static void main(String args[])
     {   
         Console c=System.console();   
System.out.println("Enter password: ");   
char[ ] ch=c.readPassword();   
String pass=String.valueOf(ch);
//converting char array into string   
System.out.println("Password is: "+pass);   
}   
} 

Output:

Read input from java console

Summary

In this Java article, we saw three different ways to get the input from the user and then process it and show the desired output to the user on the console.

We can use any of the three classes to get the console input from the user which are BufferedReader class, Scanner class, and Java Console class. There are several methods to read the input from the user. We discussed all these three ways with the example.

Thank you for reading our article. Hope you enjoyed it. Do share it on Social Media.

Keep Learning 🙂