There are times when it is important for you to get input from users for execution of programs. To do this you need Java Reading Console Input Methods.

Java Reading Console Input Methods

  1. Using BufferedReader Class
  2. Using Scanner Class
  3. Using Console Class

Now let us have a close look at these methods one by one.

Using the BufferedReader Class

This is the oldest technique in Java used for reading console input. This technique was first introduced in JDK 1.0. Using this technique we need to wrap InputStreamReader and System.in in the BufferedReader class.

This is done using the following syntax:

BufferedReader br = new BufferedReader (new InputStreamReader (System.in));

Using this will link the character based stream ‘br’ to the console for input through System.in.

NOTE: while using BufferedReader an IOException needs to be thrown else an error message will be shown at compile time.

BufferedReader Class is defined in java.io class so for using BufferedReader Class you need to import java.io first.

Let us have a look at an example to make the concept clearer.

In this example, we will get a string of characters entered from the user. The program will display the characters one by one to the user on the screen. It will continue till the termination character is encountered in the string.

 
 
// Program to read a string using BufferedReader class.
import java.io.*;
class bread
{
      public static void main(String args[]) throws IOException
      {
            char ch;
            BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
            System.out.println ("Enter any string of your choice (To terminate Press \'z\' ");
            do
            {
                  ch = (char) br.read ();
                  System.out.println (ch);
            } while (ch != 'z');
      }
}

 Output 1

Java Reading from Console

Here, when z is encountered in the string of characters, the program will stop displaying any further characters entered by the user.

Output 2

Reading Console Input in Java output

Using the Scanner Class

The Scanner class was introduced in JDK 1.5 and it has been widely used thereof. The Scanner class provides various methods for easing the way we get input from the console. Scanner class is defined in java.util so you need to import this first.

The Scanner also uses System.in and its syntax is as follows:

Scanner obj_name = new Scanner (System.in);

Some of the utility methods Scanner class provides are as follows:

hasNext() It returns a Boolean value depending on whether any token is available or not. It returns a true value if any token is available. Else it will return a false value.
hasNextInt() It returns a true if an int value is being read. Else it will return a false value.
hasNextFloat() It returns a true if a float value is being read. Else it will return a false value.
hasNextBoolean() It returns a true if a boolean value is being read. Else it will return a false value.
next() It reads the next token as a String.
nextLine() It reads the next input line as a String.
nextInt() It reads the next input value as an integer.
nextDouble() It reads the next input value as a double value.

Let us have a look at an example to understand the concept in a better way. In this example we will read numbers from a user and will find the sum of these numbers and display the result to him.

 
 
// Program to calculate the sum of n numbers using Scanner class
import java.util.*;
class scanner_eg
{
      public static void main(String args[])
      {
            Scanner obj = new Scanner (System.in);
            double total = 0;
            System.out.println ("Enter numbers to add. Enter any string to end list.");
            while (obj.hasNext())
            {
                  if (obj.hasNextDouble())
                  {
                        total += obj.nextDouble();
                  }
                  else
                  {
                        break;
                  }
            }
            System.out.println ("Sum of the numbers is " + total);
      }
}

Output

NOTE: – Whenever we use the next method, at run time it will always search for the input it defines. If such an input is not available then it will throw an exception. Hence, it is always beneficial to check the input beforehand by using the hasNext method before calling the next method.

Using the Console Class

This is another way of reading user input from the console in Java. This way of reading user input has been introduced in JDK 1.6. This technique also uses System.in for reading the input.

This technique is best suited for reading input which does not require echoing of user input like reading user passwords. This technique reads user inputs without echoing the characters entered by the user.

The Console class is defined in the java.io class which needs to be imported before using the console class.

Let us consider an example.

 
 
import java.io.*;
class consoleEg
{
      public static void main(String args[]) 
      {
            String name;
            System.out.println ("Enter your name: ");
            Console c = System.console();
            name = c.readLine();
            System.out.println ("Your name is: " + name);
      }
}

Output

using console class

NOTE: – The only drawback of this technique is that it works in interactive environments and it does not work in IDE.

In Java we can read console input in the three ways i.e. using BufferedReader class, Scanner class, and Console class in Java. Depending on which way you want to read user input, you can implement it in your programs.