
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we 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 a Reader class or Scanner class.
Example1
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
Enter a first number: 15 Enter a second number: 20 The Result is: 300
Example2
import java.util.*; public class ReadDataFromScanner { public static void main (String[] args) { int firstNum, secondNum, result; Scanner scanner = new Scanner(System.in); 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
Enter a first number: 20 Enter a second number: 25 The Result is: 500
- Related Articles
- Can we read from JOptionPane by requesting input from user in Java?
- Java Program to Read The Number From Standard Input
- How to Read The Number From Standard Input in Swift Program?
- Haskell program to read numbers from standard input
- Kotlin Program to Read The Number From Standard Input
- How many ways can we read data from the keyboard in Java?
- How can we extract the numbers from an input string in Java?
- Read a character from standard input without waiting for a newline in C++
- How can we read a JSON file in Java?
- Ways to read input from console in Java
- Can we use readUTF() to read a string from a .txt file in Java?
- How can we read data from an excel sheet in Selenium Webdriver?
- How can we read & write a file using Gson streaming API in Java?
- How can we read inputs as integers in Python?
- How to read an input value from a JTextField and add to a JList in Java?

Advertisements