
- 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
Java Program to Read The Number From Standard Input
In this article, we will understand how to read a number from standard input in Java. The Scanner.nextInt() method 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.
Below is a demonstration of the same −
Input
Suppose our input is −
55
Output
The desired output would be −
The input value is 55
Algorithm
Step1- Start Step 2- Declare an integer: value Step 3- Prompt the user to enter an integer value/ define the integer Step 4- Read the values Step 5- Display the value Step 6- Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
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
Example 2
Here, the input is being entered by the user based on a prompt and read through InputStreamReader object.
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
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
- Related Articles
- Kotlin 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
- How can we read from standard input in Java?
- Python program to read input from console
- Java Program to read the next byte of data from the input stream
- Ways to read input from console in Java
- Read a character from standard input without waiting for a newline in C++
- Java program to read numbers from users
- Java Program to Get Input from the User
- Haskell Program to read an integer number from the user
- Can we read from JOptionPane by requesting input from user in Java?
- Java Program to Check whether the input number is a Neon Number
- Way to read input from console in C#
- Java Program to Calculate Standard Deviation
