All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Read Input From Stdin In Java

Last Updated : Mar 11, 2024

How To Read Input From Stdin In Java

In this article we will show you the solution of how to read input from stdin in java, here we using eclipse tools so itself its importing necessary package during each program creation. If you use another tool then import priority package at the first line.

To achieve the result we need standard input with scanner object, these thing will get supported by ‘util.*’ package.

For that reason, we imported this package in this program. Now you can request user to enter input.

Step By Step Guide On How To Read Input From Stdin In Java :-

Stdin - refers standard input which denote by ‘system.in’ code. That is you will used inside the scanner object creation.

In main class ‘RdSin_prgm’ we provide static main method definition. There we have to create scanner object first with standard input.

Then we are displaying a message to indicate user to enter input. Then here we asking float type input from user.

Even if you are giving integer type input that will treated as float type data. At last we printed user input with some message.

//Read Stdin Input Example
package java_prgms;
import java.util.*;
class RdSin_prgm
{
 public static void main(String args[])
    {
  Scanner sobj=new Scanner(System.in);
  System.out.println("Enter Float stdinput");
         float f=sobj.nextFloat();
         sobj.close();
  System.out.println("stdinput value is "+f);
     }
}
  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. We named main class as ‘RdSin_prgm’ within that we defined public static void main method. There we created scanner object ‘sobj’ to access and taking further action in program. To accomplish your goal the system.in line passed into scanner object creation.
  3. Using println() we displaying message ‘Enter Float stdinput’, that will make the end user understand what they have to enter as input.
  4. To collect float type input we used ‘nextFloat()’ with scanner object ‘sobj’. After binding both of them, it returns user entered data that is stored on variable ‘f’.
  5. Then we closing scanner object through sobj.close(), finally we displayed result as ‘stdinput value is’ with input value.
  6. 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 take input from user through standard input in java.

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

After successful compilation you need to execute ‘java RdSin_prgm‘ line for gets the output. Then you can see message 'Enter Float stdinput', now user needs to enter input.

Final result you will get as ‘stdinput value is 5.12’. On eclipse just press of run button then you will get output inside terminal.

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