All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Take Input From User In Java Using Bufferedreader

Last Updated : Mar 11, 2024

How To Take Input From User In Java Using Bufferedreader

In this article we will show you the solution of how to take input from user in java using bufferedreader, first package will imported by eclipse and all program will import itself during creation. Remaining imported packages supports the given buffered class, exception handling and input stream codes.

Here showing guides you to collect the input through buffered reader input stream and exceptions.

Step By Step Guide On How To Take Input From User In Java Using Bufferedreader :-

The buffered reader make the performance faster and creates internal array to store text gets from user.

When you wrapping standard input stream with input stream helps to collect input from user. The IOException extends for handling the thrown error during compile or run time.

In main class ‘BufRdr_prgm’, we provided main method definition that’s extends with IOException.

At the foremost step you need to create bufferedreader object to proceed with further process. Now you can get user input with the help of inputstreamreader.

//GET INPUT FROM USER
package java_prgms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class BufRdr_prgm
{
 public static void main(String args[]) throws IOException
    {
        BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter Your Name");
        String name=r.readLine();
  System.out.println("Your name is : "+name);
    }
}
  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 defined main class named as ‘BufRdr_prgm’, which contains static main method definition. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. In main method first we creating bufferedreader object by passing standard input stream of ‘system.in’ to inputstreamreader during creation of object.
  4. Then its object known as variable ‘r’, then you need to display text ‘Enter Your Name’. It will notifies user to enter some input in detail. When you binding ‘readLine’ statement with object ‘r’ asking user to enter input.
  5. Then entered user input stored on string variable ‘name’ and displayed that result with text ‘Your name is : prawin’. It will vary depends on different end user input and you can modify sample text, defined inside println() method.
  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 by bufferedreader in java.

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

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

Then you can see output ' Your name is : prawin'. On eclipse just press of run button then you will get output this inside terminal.

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