All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Take Character Input In Java Using Bufferedreader

Last Updated : Mar 11, 2024

How To Take Character Input In Java Using Bufferedreader

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

Remaining imported packages gives support to the bufferedreader, exception handling and input stream codes defined inside program.

Here showing example teach you to collect the character input and displayed at terminal.

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

The buffered reader makes the performance faster and creates internal array to store result of gets from user.

Wrapping standard input stream of ‘System.in’ inside input stream reader helps to collect input from user during creation of buffered reader.

The IOException extends with main method for handling the thrown error if fails at any situation.

The main class ‘BufChr_prgm’, contains main method definition that’s needs to extends with IOException. Then you need to create bufferedreader object to proceed with further process.

And then only you can read character input with the help of inputstreamreader.

//GET CHARACTER INPUT FROM USER
package java_prgms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class BufChr_prgm
{
 public static void main(String args[]) throws IOException
    {
        BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter Any Character");
         char ch=(char)bfr.read();
  System.out.println("You input character is : "+ch);
    }
}
  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. In main class ‘BufChr_prgm’, we provide static main method definition and that must extends with IOException to run without error. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. Within this first you need to create bufferedreader object ‘bfr’ by passing inputstreamreader with argument of standard input stream ‘system.in’.
  4. To intimate user to enter character input at terminal we displaying ‘Enter Any Character’ this message by println() method.
  5. Then for collect character input you need to specify datatype near object ‘bfr’ within bracket. Which needs to appends with read() that will request user to enter input.
  6. At last we displayed result with text ‘You entered input character is : w’. It will might change based on end user and you can alter the sample text, which defined inside println() methods in program.
  7. 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.
  8. 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 character input in java with the help of bufferedreader.

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

After successful compilation you need to execute ‘java BufChr_prgm‘ line for gets the output. Then you can see output 'You entered input character is : w'.

On eclipse just press of run button then you will get output this inside terminal.

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