Site icon TestingDocs.com

Java program to read an integer

Program Description

Write a java program to read an integer entered by user. nextInt() method of the Scanner class will read the user input integer from the Standard input ( System.in) . We will declare a variable to hold the integer in our Java program.

 

Java Program

import java.util.Scanner;
/***********************************************************
 * IntegerDemo.java
 * @program : Java program to read an integer entered by user
 * @web : www.testingdocs.com
 * @version : 1.0
 ************************************************************/ 
public class IntegerDemo {

  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);
  }
}

 

Program Output

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

Screenshot

 

Exit mobile version