BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to read integer value from the Standard Input

Last Updated: October 25, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this program we will see how to read an integer number entered by user. Scanner class is in java.util package. It is used for capturing the input of the primitive types like int, double etc. and strings.

Example: Program to read the number entered by user

We have imported the package java.util.Scanner to use the Scanner. In order to read the input provided by user, we first create the object of Scanner by passing System.in as parameter. Then we are using nextInt() method of Scanner class to read the integer. If you are new to Java and not familiar with the basics of java program then read the following topics of Core Java:
→ Writing your First Java Program
→ How JVM works

import java.util.Scanner;
public class Demo {

  public static void main(String[] args) {

    /* This reads the input provided by user
     * using keyboard
     */
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter any number: ");

    // This method reads the number provided using keyboard
    int num = scan.nextInt();

    // Closing Scanner after the use
    scan.close();

    // Displaying the number
    System.out.println("The number entered by user: "+num);
  }
}

Output:

Enter any number: 101
The number entered by user: 101

Recommended Posts

  • Java program to get input from user
  • Java program to add two numbers
  • Java packages and how to use them

Top Related Articles:

  1. Java Program to Add two Numbers
  2. Java Program to Calculate average using Array
  3. Sunny Number Program in Java
  4. Peterson Number Program in Java
  5. Sphenic Number in Java – Check and Print all numbers in a range

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap