Write a Java program to print an Integer

How to print an integer entered by an user

In this program, an object of scanner class, reader is created to take input from standard input, Which is keyboard.

Then, Enter a number prompt is printed to give the user a visual cue as to what they should do next.

reader.nextInt() the reads all entered integers from the keyword unless it encounters a new line character \n Enter. The entered integer are then saved to the integer variable number.

If you enter any character which is not an integer, the compiler will throw an InputMismatchException.

Finally, number is printed onto the standard output (system.out) - computer screen using the function println().

  •  Program:

import java.util.Scanner;

public class HelloWorld {

    public static void main(String[] args) {

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

        // nextInt() reads the next integer from the keyboard
        int number = reader.nextInt();

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

  •  Output:

Enter a number: 10
You entered: 10

  •  Visit:

Write a Java program to add two integers Here

0 comments