
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Read Number from Standard Input in Java
In this article, we will understand how to read a number from standard input in Java. The Scanner.nextInt() method of java.util package is used to read the number. The java.util.Scanner.nextInt() method scans the next token of the input as an int. An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.
Problem Statement
Write a Java program to read the number from standard input. Below is a demonstration of the same ?
Input
55
Output
The input value is 55
Different Approaches
Below are the 2 approaches to read the number from the standard input ?
Using Scanner Class
The following are the steps to read the number from the standard input using Scanner class ?
- Import the necessary classes from java.util package
- Create a class named PrintNumber.
- Declare an integer variable to store the input value.
- Instantiate the Scanner class to read input from the user.
- Display a message asking the user to enter a number.
- Use nextInt() method from the Scanner class to read the integer input.
- Print the entered number to the console.
Example
Here, the input is being entered by the user based on a prompt ?
import java.util.Scanner; public class PrintNumber{ public static void main(String[] args){ int value; System.out.println("Required packages have been imported"); System.out.println("Variable to store value is defined"); Scanner reader = new Scanner(System.in); System.out.println("A reader object has been defined\n"); System.out.print("Enter a number: "); value = reader.nextInt(); System.out.println("The nextInt method is used to read the number "); System.out.println("The number is: "); System.out.println(value); } }
Output
Required packages have been imported Variable to store value is defined A reader object has been defined Enter a number: 55 The nextInt method is used to read the number The number is: 55
Using BufferedReader Class
The following are the steps to read the number from the standard input using the BufferedReader class ?
- Import necessary classes import java.io package.
- Create a class named readNum.
- Create an InputStreamReader object which reads bytes and decodes them into characters.
- After that will create a BufferedReader object which wraps the InputStreamReader to buffer the input for efficient reading.
- Ask the user for input.
- We will use readLine() from BufferedReader to read the input and Integer.parseInt() to convert it to an integer.
- Print the entered number to the console.
Example
Here, the input is entered by the user based on a prompt and read through InputStreamReader object ?
import java.io.*; public class readNum{ public static void main(String args[]) throws IOException{ InputStreamReader read=new InputStreamReader(System.in); System.out.println("An object of InputStreamReader class is created"); BufferedReader in=new BufferedReader(read); System.out.println("A constructor of the BufferedReader class is created"); System.out.println("Enter a number: "); int number=Integer.parseInt(in.readLine()); System.out.println("The number is : "+number); } }
Output
An object of InputStreamReader class is created A constructor of the BufferedReader class is created Enter a number: The number is : 45