Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Read from Standard Input in Java



The standard input (stdin) can be represented by System.in in Java. The System.in is an instance of the InputStream class. It means that all its methods work on bytes, not Strings. To read any data from a keyboard, we can use either the Reader class or the Scanner class.

Using Reader Class

In Java, the Reader class is an abstract class belonging to the java.io package that is used for reading character streams. It is the superclass of all character input streams, such as "BufferedReader", "InputStreamReader", "FileReader", etc.

Subclasses of Reader

To use the functionality of the Reader class, use its subclasses ,which are:

  • BufferedReader
  • InputStreamReader
  • FileReader
  • StringReader

Example

The following example uses the InputStreamReader subclass of the Reader class to read from standard input -

import java.io.*;
public class ReadDataFromInput {
   public static void main (String[] args) {
      int firstNum, secondNum, result;
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      try {
         System.out.println("Enter a first number:");
         firstNum = Integer.parseInt(br.readLine());
         System.out.println("Enter a second number:");
         secondNum = Integer.parseInt(br.readLine());
         result = firstNum * secondNum;
         System.out.println("The Result is: " + result);
      } catch (IOException ioe) {
         System.out.println(ioe);
      }
   }
}

Output

The above program produces the following output -

Enter a first number:
15
Enter a second number:
20
The Result is: 300

Using Scanner Class

In Java, the Scanner class is part of the java.util package is used to read input from various sources like keyboard input (standard input), files, strings, etc.

Syntax of the Scanner Class

Following is the syntax of the Scanner class in Java -

Scanner sc = new Scanner(System.in)

Here, sc is the reference variable (or object) of the Scanner class.

Example

Another way to read from standard input is by using the Scanner class. To do this, we need to import the java.util.Scanner package and create an instance of the Scanner class -

import java.util.Scanner;
public class ReadDataFromScanner {
   public static void main (String[] args) {
      //instantiate Scanner class
      Scanner scanner = new Scanner(System.in);

      int firstNum, secondNum, result;
      System.out.println("Enter a first number:");
      firstNum = Integer.parseInt(scanner.nextLine());

      System.out.println("Enter a second number:");
      secondNum = Integer.parseInt(scanner.nextLine());

      result = firstNum * secondNum;
      System.out.println("The Result is: " + result);
   }
}

Output

Following is the output of the above program -

Enter a first number:
20
Enter a second number:
25
The Result is: 500
Updated on: 2025-05-29T19:05:27+05:30

36K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements