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

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

Leave a Reply Cancel reply

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

Programs

  • C Programs
  • Java Programs
  • C++ Programs

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 – 2022 BeginnersBook . Privacy Policy . Sitemap