In this post, we will learn how to print an integer entered by the user using Java Programming language.
This program asks the user to enter a number, then it displays the entered number using the System.out.print statement.
So, without further ado, let’s begin this tutorial.
Java Program to Print an Integer Entered by the User
// Java Program to Print an Integer Entered by the User import java.util.Scanner; public class NumberPrint{ public static void main(String[] args){ // Asking for input Scanner reader = new Scanner (System.in); System.out.println("Enter an integer: "); int num = reader.nextInt(); // Displaying output System.out.println("You entered: " + num); } }
Output
Enter an integer:
15
You entered: 15
How Does This Program Work ?
// Asking for input Scanner reader = new Scanner (System.in);
In this program, we have created a Scanner reader class to take inputs.
System.out.println("Enter an integer: ");
Then, the user is displayed a message to enter an integer.
int num = reader.nextInt();
Now, we used reader.nextInt() to read all the entered integers. The entered integer gets stored in the num named variable.
// Displaying output System.out.println("You entered: " + num);
Finally, the entered number is displayed on the screen using the System.out.println() function.
Conclusion
I hope after going through this post, you understand how to print an integer entered by the user using Java Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to solve your query.
Also Read: