All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Input From User In Java

Last Updated : Mar 11, 2024

How To Get Input From User In Java

In this article we will show you the solution of how to get input from user in java, first package will imported by eclipse and all program will import itself during creation.

Second imported package ‘util.*’ must to request user for input at execution time.

In main class we defined static main method, there we showing example of getting input from user and displayed that value as output at terminal.

Step By Step Guide On How To Get Input From User In Java :-

In main class ‘GetInp_prgm’, we defined main method definitions. Within that you need to create scanner object then only can achieve the target result.

Using scanner object you can collect any type of input from end user such as float,int,string and so on.

Here given example will teach to get integer data type input from user and displayed final answer through printing on terminal.

//GET INPUT FROM USER
package java_prgms;
import java.util.*;
class GetInp_prgm
{
 public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
  System.out.println("Enter Integer Number");
        int num=sc.nextInt();
  System.out.println("You entered input is : "+num);
    }
}
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. The main class named as ‘GetInp_prgm’, which contains static main method definitions. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. In main method you might create scanner object ‘sc’ at foremost step and printed text ‘Enter Integer Number’ by println() method for intimate user to enter input.
  4. To getting integer data type we provide ‘nextInt’ with object ‘sc’ and storing collecting value on variable ‘num’.
  5. You can collect some other data type input from user, but that will need proper modification. To figure out that modification you need to stay tune for next tutorial.
  6. Again using println() method we printing result as ‘You entered input is : 24’.The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  7. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to collect integer input from user in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac GetInp_prgm.java’.

After successful compilation you need to execute ‘java GetInp_prgm‘ line for gets the output.

Then you can see output ' You entered input is : 24'. On eclipse just press of run button then you will get output inside terminal.

The output will change based on different end user and you can modify the text above provide.

I hope this article on how to get input from user in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪