Javatpoint Logo
Javatpoint Logo

Console in Java

In this section, we will learn everything about the Console in Java, i.e., what a console is, how we can use the console, how we can implement output in the console, how we can take input using the console, etc.

What is a Console?

To run a program, we might need input from the programmer or user according to the requirement. We cannot always take input just from the program. Sometimes, we can take the input from the console or terminal too. The process of taking input from the console is introduced by the concept of Java Console. Java programming language provides several ways in order to take input from the console and provide its corresponding output on the same console.

Java Console

Using the Console in Java, we can consider taking input and displaying the output. Generally, Console is mainly meant for taking input. There are three ways to take the input from the Java console. They are:

  1. Using Java Scanner class (Basic level)
  2. Using Java BufferedReader class (Intermediate level)
  3. Using Java Console class

Scanner class in Java

A class that is used to scan inputs within the program is known as the Scanner class. We use this class to take the input within the program by creating an abject in the Scanner class. It is one of the easiest ways to scan the input from the user using the console. It is used to scan all the regular expressions in Java. The Scanner class is available in the util package. So, in order to scan the inputs using the Scanner class, we need to import the " Java.util " package.

Syntax of Scanner class:

Here, " Scanner " is considered as a class and " obj " is an object that is created within a class. So, in order to scan any input in the entire program, we can use this object that is created in the " Scanner " class.

There are different methods used for scanning different data types. The type of method that we consider depends on the data type of the input that we want to take. There are eight methods in the Scanner class, as there are eight general Primitive data types.

Methods in Scanner class:

  1. nextByte()
    The method that deals with reading a Byte value from the user are nextByte()
  2. nextDouble()
    The method that deals with reading a Double value from the user are nextDouble()
  3. nextFloat()
    The method that deals with reading a Float value from the user are nextFloat()
  4. nextInt()
    The method that deals with reading an Int value from the user are nextInt()
  5. nextLine()
    The method that deals with reading a String value from the user are nextLine()
  6. nextLong()
    The method that deals with reading a Long value from the user are nextLong()
  7. nextShort()
    The method that deals with reading a Short value from the user are nextShort()
  8. nextBoolean()
    The method that deals with reading a Boolean value from the user are nextBoolean()

A program that demonstrates Scanner class to scan an input:

Input:

Output:

You have entered an Integer number 
5
You have entered a Float number
3.4
You have entered a String
Hello
You have entered a Boolean value
true

An explanation for the above Program and its output:

Initially, the required package " util.Scanner " is imported in order to access and implement the class " Scanner ". A class " Demo " is considered and declared in which the main section is also declared. Under the main section, an object " obj " is created with respect to the class " Scanner " in order to scan the elements using that object " obj ". After the creation of an object " obj " is successful, methods are called by the object one by one according to the data types declared. For example, " x " is a variable in which the object " obj " called the method " nextInt() " which is used to scan the integer values. Similarly, the rest of the variables are assigned with the object calling their particular methods. Finally, all the values are allowed to be printed using the print statement. This is how the " Scanner " class works.

Advantages of Scanner class:

  • This class provides several methods according to the data type used and that match the user's requirement.
  • It is the basic class used to scan an input that runs on almost all versions of Java.

Disadvantages of Scanner class:

  • It runs and executes a little slower as the data must be parsed before being executed.
  • The methods that are part of the class " Scanner " are not synchronized.

BufferedReader class in Java:

It is one of the classical methods of taking input from the user ( sometimes by using a console also ). A new statement, " InputStreamReader " is also introduced along with the " BufferedReader " in order to scan the input values using BufferedReader. Let us look at the mechanism of BufferedReader with its syntax followed by a program.

Syntax of BufferedReader class:

Methods in BufferedReader class

  1. readLine()
    The method " readLine() " is used to scan the String values from the user.
  2. parseInt()
    The method " Integer.parseInt() " is used to scan the integer values from the user.
  3. parseDouble()
    The method " Double.parseDouble() " is used to scan the double values from the user.
  4. parseFloat()
    The method " Float.parseFloat() " is used to scan the float values from the user.

A program that demonstrates BufferedReader class to scan an input:

Input:

Output:

The entered String is: Hello
The entered value of integer is: 10
The entered value of float is: 3.5
The entered value of double is: 9.88

An explanation for the above Program and its output:

Initially, the required packages " io.BufferedReader ", " io.InputStreamReader ", and the general package " io.* " are imported in order to access and implement the class " BufferedReader " with the help of the supporting class " InputStreamReader ". A public class " Demo " is considered and declared in which the main section is also defined. Under the main section, an object " obj1 " is created with respect to the class " InputStreamReader " in order to support the scanning of elements in the class " BufferedReader " and by using the object " obj1 ", we can initiate the scanning class " BufferedReader ".

After the creation of an object " obj1 " is successful, the scanning class " BufferedReader " is initiated. Another object, " obj2 " is created in which the object of " InputStreamReader ", i.e., " obj1 " is taken as a supplementary argument. The methods are called by the object created in the BufferedReader class, i.e., " obj2 " one by one according to the data types declared. For example, " s " is a variable in which the object " obj2 " called the method " readLine() " which is used to scan the String. Similarly, the rest of the variables are assigned with the object, calling their particular methods. Finally, all the values are allowed to be printed using the print statement. This is how the " BufferedReader " class works.

Advantages of BufferedReader class:

  • All the methods that are provided by this class are completely synchronized.
  • It is the mandatory class used to scan an input, mainly during the " Multi-threading " concept.
  • This class has a large Buffer compared to the Scanner class.
  • The speed of execution is faster when compared to the Scanner class.

Disadvantages of BufferedReader class

The methods present in the BufferedReader class are not in-built but should be parsed while using.

Java Console class in Java

The class " Console " in Java was introduced from Java version 1.5 and was brought into charge from that version. It was one of the most anticipated usages in Java Programming language. The class " Console " can be accessed through the package " Java.io " which is the basic package that is used in all programs constructed in Java. There are several methods that are embedded within the " Console " class. Let us study all those methods in the upcoming statements.

One of the major advantages of using the class " Console " in Java is that the confidential data or information like passwords are hidden while taking input as well as printing output. This is the major reason for preferring the Console class in Java. Not only this feature, but the methods of the console that can be read are also always synchronized. But, the Console class can only be executed on command prompt or terminal but not on any other IDE platforms. Basically, the class " Console " in Java does not have a specified syntax. The syntax and usage of the " Console " class change depending on the purpose and situation the program is being performed.

Methods in Console class:

  1. readLine()
    The method that is used to read a single line of text or String from the console is the " readLine() " method. This method can be accessed or used when an object is created within the String class.
  2. readLine( String, Object )
    The method that is used to provide a prompt that is formatted priorly in order to receive an input from the console is the " readLine( String, Object ) " method. This method can be used when an object is created within a String class. The only difference between the method " readLine() " and the method " readLine( String, Object ) " is the type and number of arguments that are being passed through those methods.
  3. readPassword()
    The method that is used to read or scan a password as an input in a way such that the input taken is shown or visible at any time on the screen is the " readPassword() " method. The data type that is supposed to be considered in this method is a Character array or " char[] ".
  4. readPassword( String, Object )
    The method that is used to read or scan a password as an input in a confidential way such that the input taken is shown or visible any time on the screen along with providing a formatted prompt is the " readPassword( String, Object ) " method. The data type that is supposed to be considered in this method is a Character array or " char[] ".
  5. reader()
    The method is called by an object created in the class " Reader() " which handles and relates to the console. This method also helps in the creation of the object in the class " Reader() ".
  6. Console printf( String, Object )
    The method that is used in order to print a string within the output of the console is " printf() ". It can be accessed using the class " Console() ".
  7. flush()
    The method that is used to clear all the statements or the matter present on the console is " flush() ". It is a general method which is neither creates an object nor will be accessed by an object created within a particular class. It just removes the matter on the console screen. Its return data type is generally " void ".
  8. Console format( String, Object )
    The method that prints a formatted string ( when asked for print ) on the output screen of the console is the " format( String, Object ) " method. We generally use this in order to have a classified string that is arranged perfectly as per the requirement. This method can be accessed and is also a part of the class " Console ".

Not only these methods, but there are also several other methods that are a part of the class " Console ". The usage of the methods depends on the way the program runs and the operation that the program wants to perform.

A program that demonstrates Console class to scan an input:

Input:

Output:

You can enter an input using Console now!! 
Enter the first input: Hello
The String that you have entered is : 
Hello
Let us find the reader object of the Console class:
The reader object of the Console class is:
Java.io.Console$LineReader@1be6f5c3

Input 2:

Output 2:

You can enter an input using Console now!! 
Enter the first input: World
The String that you have entered is : 
World
Let us find the reader object of the Console class:
The reader object of the Console class is:
Java.io.Console$LineReader@1be6f5c3

An explanation for the above Program and its output:

Initially, the required packages " Java.io.Console " and the general package " io.* " are imported initially in order to access and implement the class " Console ". A public class, " Demo2 " is considered and declared in which the main section is also defined. Under the main section, the class " Console " is considered in which an object " c " is created in order to support the scanning of elements in the class " Console " and by using the object " c ", we can initiate the scanning class " Console ".

After the creation of an object " c " is successful, the scanning class " Console " is initiated, and the corresponding methods of the Console class are considered according to the requirement. The methods are called by the object created in the class " Console ", i.e., " c " one by one according to the data types declared and the type of operation that you want to perform. For example, if you want to take a String as an input, you need to access the method " readLine() " which is already done in the above program. " x " is a String variable in which the input taken by the object " c " by calling the method " readLine() " is stored.

This process or the method is used to scan the String. Similarly, the rest of the variables, i.e., " r " created in the " Reader " is assigned such that the reader object of the Console class is stored. In this way, the variables are assigned with an object, calling their particular methods. Finally, all the values are allowed to be printed using the print statement. This is how the " Console " class works.

A program that demonstrates Console class to scan a password:

Input 1:

Output 1:

You can enter a password using Console now!! 
Enter the input password: 
The password that you have entered is: nikki@1234567890

Input 2:

Output 2:

You can enter a password using Console now!! 
Enter the input password: 
The password that you have entered is: nikhitha#1234567890

An explanation for the above Program and its output:

Initially, the required packages " Java.io.Console " and the general package " io.* " are imported initially in order to access and implement the class " Console ". A public class " Demo1 " is considered and declared in which the main section is also defined. Under the main section, the class " Console " is considered in which an object " c " is created in order to support the scanning of elements in the class " Console " and by using the object " c ", we can initiate the scanning class " Console ".

After the creation of an object " c " is successful, the scanning class " Console " is initiated, and the corresponding methods of the Console class are considered according to the requirement. The methods are called by the object created in the class " Console ", i.e., " c " one by one according to the data types declared and the type of operation that you want to perform. As the motive of the program is to taken an input of the password, we need to use the method " readPassword() ". For example, in the previous program, you wanted to take a String as an input, so you accessed the method " readLine() ". " x " is a String variable in which the input taken by the object " c " by calling the method " readLine() " is stored. In the same way, a variable " pass " is declared with the data type " char[] " and is used to take input with the help of the method " readPassword() ".

As the password is very confidential, while taking the input of the password, the input will not be shown. In order to know the password, we need to access and call the method " valueOf() " which is a member of the basic class " String ". This process or the method is used to scan the Password without revealing its value. In order to store the value of the password, a string variable " x " is declared. In this way, the variables are assigned with an object, calling their particular methods. Finally, all the values are allowed to be printed using the print statement. This is how the " Console " class works for scanning a Password. This way or method of usage can prevent unauthorized access by other people, excluding the user as the password cannot be seen.

Advantages of Console class

  • This class provides a method in which that passwords can be taken as input without displaying the value of the password while scanning.
  • The methods of the class "Console" are all synchronized.

Disadvantages of Console class

  • The working of the class " Console " is bound only to the terminal or the Console but not to the IDE, which is a non-interactive environment.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA