All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Take Input Using Bufferedreader In Java

Last Updated : Mar 11, 2024

How To Take Input Using Bufferedreader In Java

In this article we will show you the solution of how to take input using bufferedreader in java, with the BufferedReader class to quickly read text input from the keyboard and other input devices is referred to as "taking input using BufferedReader in Java".

It entails establishing an instance of the class by supplying an instance of an InputStreamReader to its constructor, reading the input text as a String using the readLine() function, and then using the appropriate methods to convert the String to the correct data type.

Now let's discuss the idea of using a Java bufferedreader to accept input.

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
class takingInputfromKeyBoardBufferedReader{
    public static void main(String args[]){
        float PI = 3.1416f;
        int r=0;
        String rad;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Radius?");
        try{
            rad = br.readLine();
            r = Integer.parseInt(rad);
            System.out.println("Circle area is: " + PI*r*r + " Permieter: " +PI*2*r);
        }
        catch(Exception e){
            System.out.println("Write a positive integer");
            InputUsingBufferedReader a = new InputUsingBufferedReader();
        }
    }
}
  1. As you can see, we have written some simple Java code that uses BufferedReader to read keyboard input and calculate a circle's area and perimeter based on the user's input.
  2. The necessary Java classes, java.io.BufferedReader & java.io.InputStreamReader, are imported at the beginning of the code.
  3. The main method is then specified, and a class called takingInputfromKeyBoardBufferedReader is defined.
  4. The subsequent declaration and initialization of the following variables occurs in the main method: PI is a float variable initialised to 3.1416f that stores the value of pi. rad: The user's input will be saved as a string in the String variable. r: an int variable to hold the circle's radius, initialised to 0.
  5. Then, using System.in as the input stream, a BufferedReader class instance is built.
  6. The user is then prompted with a message requesting them to enter the circle's radius using System.out.println.
  7. After that, a try-catch block is used to receive the user's input & calculate the circle's area and perimeter.
  8. The user input is read as a string and saved in the rad variable using the readLine() function of the BufferedReader class.
  9. The string is then turned into an integer using the parseInt() method, and it is then saved in the r variable.
  10. The formulas PI*r*r and PI*2*r are then used to get the circle's area and circumference, respectively.
  11. Results are then shown using System.out.println.
  12. A message prompting the user to provide a positive integer is displayed if an exception occurs in the catch block.
  13. After that, a fresh instance of the class takingInputfromKeyBoardBufferedReader is created in order to restart the input procedure.

Conclusion :-

As a result, we have mastered the Java concept of accepting input through a bufferedreader.

Additionally, we discovered that the code uses the Java BufferedReader class to compute a circle's area and perimeter after converting user input to an integer.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪