In this tutorial you will learn about the Java Program to Get User Input and Print on Screen and its application with practical example.
Java Program to Get User Input and Print on Screen
In this tutorial, we will learn to create a Java program that will get user input and print on screen in Java programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
What is a data type?
A data type, in every programming language, specifies which type of value a variable is going to hold in the program. i.e. integer, character, float, double.
Algorithm:-
1 2 3 4 5 6 7 |
1. Declaring the variables for the program. 2. Defining the input value for the variable in number format. 3. Printing the data taken in input from the user in integer format. 4. End the program. |
Program Print output to the user.
In this tutorial, First, we will take input from the user in number format using a scanner class object in the program. Then we will store it in a variable in the memory. Then we will print that number to the user using the print function.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Program to import java.util.Scanner; public class IntData {//Creating the class for the program in public type //Body of Main function of the program public static void main(String[] args) { //Declaring the required variable for the program. //Integer type variable to hold number value. // Creates a reader instance which takes // input from standard input - keyboard int no; //Scanner is imported from the import package Scanner reader = new Scanner(System.in); //Taking the input from the user System.out.print("Enter a number: "); // nextInt() reads the next integer from the keyboard no = reader.nextInt(); // println() prints the following line to the output screen //Using the System.out.println(); System.out.println("You entered: " + no); } } |
Output:-
In the above program, we have first initialized the required variable.
- no = it will hold the input integer value from the user.
Importing the scanner in util package for the program.
Taking the input from the user in integer format.
Printing the integer value to the user by print function.