Taking integer input in Java

Program Description

This short program demonstrates,how to accept integer inputs from the user and store them in a variable.
Since for taking input from the user,we need to scan the keyboard, we've included the scanner class which has the methods which handle this task for us.
We simply need to use these as shown in the code example below.

Program
/*****************************************
*   Taking integer input in Java
*
*    code.cheraus.com
******************************************/


import java.util.Scanner;
class get_var {
               public static void main(String args[])
               {
            	   Scanner var = new Scanner(System.in);
            	   System.out.print("Enter the number : ");
            	   int num = var.nextInt();
            	   
            	   System.out.println("The entered number is " + num);
               }
}

Output
Test Case 1:

Enter the number : 45
The entered number is 45

Test Case 2:

Enter the number : 415
The entered number is 415

Comments
comments powered by Disqus