Wednesday, July 3, 2024
HomeLanguagesJavaHow to Read and Print an Integer value in Java

How to Read and Print an Integer value in Java

The given task is to take an integer as input from the user and print that integer in Java language. 

In the below program, the syntax and procedures to take the integer as input from the user are shown in Java language.

Steps for Input

  1. The user enters an integer value when asked.
  2. 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 of Scanner Class

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.

Steps for Output

  1. This entered value is now stored in the variableOfIntType.
  2. 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 of println

System.out.println(variableOfXType);

Hence, the integer value is successfully read and printed.

Program Read and Print an Integer value in Java

Below is the implementation of the above method:

Java




// Java program to take an integer
// as input and print it
import java.io.*;
import java.util.Scanner;
 
// Driver Class
class GFG {
      // main function
    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);
    }
}


Input

Enter the integer: 10

Output

Entered integer is: 10
Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments