How to Read and Print an Integer value in Java - GeeksforGeeks

05 Dec, 2018
The given task is to take an integer as input from the user and print that integer in Java language.
In below program, the syntax and procedures to take the integer as input from the user is shown in Java language.
Steps:
The user enters an integer value when asked.This value is taken from the user with the help of nextInt() method of Scanner Class. The nextInt() method, in Java, reads the next integer value from the console into the specified variable.Syntax:variableOfIntType = ScannerObject.nextInt();

where variableOfIntType is the variable
in which the input value is to be stored.
And ScannerObject is the beforehand created 
object of the Scanner class.
This entered value is now stored in the variableOfIntType.Now to print this value, System.out.println() or System.out.print() method is used. The System.out.println() method, in Java, prints the value passed as the parameter to it, on the console screen and the changes the cursor to the next line on the console. Whereas System.out.print() method, in Java, prints the value passed as the parameter to it, on the console screen and the cursor remains on the next character of the last printed character on the console.Syntax:System.out.println(variableOfXType);
Hence, the integer value is successfully read and printed.
The user enters an integer value when asked.
This value is taken from the user with the help of nextInt() method of Scanner Class. The nextInt() method, in Java, reads the next integer value from the console into the specified variable.Syntax:variableOfIntType = ScannerObject.nextInt();

where variableOfIntType is the variable
in which the input value is to be stored.
And ScannerObject is the beforehand created 
object of the Scanner class.

Syntax:
variableOfIntType = ScannerObject.nextInt();

where variableOfIntType is the variable
in which the input value is to be stored.
And ScannerObject is the beforehand created 
object of the Scanner class.

This entered value is now stored in the variableOfIntType.
Now to print this value, System.out.println() or System.out.print() method is used. The System.out.println() method, in Java, prints the value passed as the parameter to it, on the console screen and the changes the cursor to the next line on the console. Whereas System.out.print() method, in Java, prints the value passed as the parameter to it, on the console screen and the cursor remains on the next character of the last printed character on the console.Syntax:System.out.println(variableOfXType);

Syntax:
System.out.println(variableOfXType);

Hence, the integer value is successfully read and printed.
Program:
Java
// Java program to take an integer// as input and print it  import java.io.*;import java.util.Scanner;  class GFG {    public static void main(String[] args)    {          // Declare the variables        int num;          // Input the integer        System.out.println("Enter the integer: ");          // Create Scanner object        Scanner s = new Scanner(System.in);          // Read the next integer from the screen        num = s.nextInt();          // Display the integer        System.out.println("Entered integer is: "                           + num);    }}
Output:
Enter the integer: 10
Entered integer is: 10

Java-I/O
Java-Integer
Java
School Programming
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
Python Dictionary
Arrays in C/C++
Inheritance in C++
C++ Classes and Objects
Interfaces in Java