Java program to print an Integer

Write a java program to print an Integer. User should enter input using keyboard.

import java.util.Scanner;

public class HelloTecnoWorld {

    public static void main(String[] args) {

        // Creates an instance which takes
        // input from standard input from keyboard
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter any number: ");

        // nextInt() is to read the next integer input from keyboard
        int number = reader.nextInt();

        // println() prints the below line to the output console
        System.out.println("You have entered: " + number);
    }
}

Output

Enter any number: 11
You have entered: 11

This Program is for sole knowledge of Scanners and how to take any input from the user(keyboard).

Scanner class is present in java.util package and line Scanner reader = new Scanner(System.in) is used to create an object of Scanner class. Also, argument System.in is used to tell take the input from user.

After that nextInt() is to read the next integer input from keyboard.

Leave a Reply