All TalkersCode Topics

Follow TalkersCode On Social Media

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

Example Of Bufferedreader In Java With Inputstreamreader

Last Updated : Mar 11, 2024

Example Of Bufferedreader In Java With Inputstreamreader

In this article we will show you the solution of example of bufferedreader in java with inputstreamreader, when reading text from character input stream and buffering the characters into an array, Java uses a class called BufferedReader.

It offers a productive method for reading massive amounts of text via an input source, such as a file and network connection.

A class called InputStreamReader receives bytes via an input stream & encodes them as characters according to a given character set.

It functions as a link between character and bytes streams. We will now discuss the idea of a Java buffered reader example with inputstream read.

Step By Step Guide On Example Of Bufferedreader In Java With Inputstreamreader :-

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadInput {
 public static void main(String[] args)throws IOException {
  int age = 0;
  boolean married = false;
  String gender = null;
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  try {
   System.out.println("enter age:");
   age = Integer.parseInt(br.readLine());
   System.out.println("eneter married :");
   married = Boolean.parseBoolean(br.readLine());
   System.out.println("enter gender :");
   gender = br.readLine();
  }
  catch(Throwable e) {
   e.printStackTrace();
  }
  System.out.println("enter age:" +age);
  System.out.println("eneter married :" +married);
  System.out.println("enter gender :" +gender);
  perdetails (age, gender, married);
 }
 private static int perdetails(int age, String gender, boolean married) {
  int premimum = 300;
  if((age < 25) && (gender.equals("male")) && (!married)){
   premimum = premimum + 1000;
  }
  else{
   if (married || gender.equals("female")){
    premimum = premimum - 100;
    if((age >= 46) && (age <= 65)){
     premimum = premimum - 150;
    }
   }
  }
  System.out.println("premimum" + premimum);
  return premimum;
 }
}
  1. In this example, you can see how the BufferedReader class obtained from the java.io package is used to create a straightforward Java programme that reads user input.
  2. The computer programme scans the user's ages, marital status, & gender and then uses that information to compute an insurance premium.
  3. The ages, marital status, & gender variables are first initialised to default values by the programme.
  4. Then, it creates an object called br that is a BufferedReader and reads data from the System.in standard input stream.
  5. The programme then asks the user to enter their age with the statement System.out.println("Enter age:"); before reading the input with the statement Integer.parseInt(br.readLine()).
  6. The age variable contains the user's age.
  7. The System.out.println("Enter married:"); command is used to prompt the user to enter their marital status, and the Boolean.parseBoolean(br.readLine()) statement is used to read the input.
  8. The married variable holds the user's marital status.
  9. A System.out.println("Enter gender:"); statement asks the user to enter their gender, and the br.readLine() statement receives the input. The programme then asks the user to enter their gender.
  10. The gender variable contains the user's gender information.
  11. The programme then uses the user's age, marital status, & gender as parameters when calling the perdetails() method, and it saves the result value in a variable called premimum.
  12. A user's age, marital status, & gender are used as arguments by the perdetails() method to determine an insurance premium.
  13. The method first sets the value of premimum, a variable, to 300.
  14. The system then determines if the user is a guy under an age of 25 who is not married, and if this is so, it increases the minimum amount by 1000.
  15. The procedure deducts 100 from the minimum value if the user is a married or female.
  16. Within the ages of 46 & 65, the technique deducts another 150 from the minimum value.
  17. A System.out.println("premimum" + premimum); command is then used to print the final premimum value to the console before the procedure returns.
  18. A user's age, marital status, & gender are then printed to the console by the programme using System.out.println() commands, along with the minimum value returned via the perdetails() method.

Conclusion :-

Thus, we were able to understand the idea of a Java example of a buffered reader using an inputstream reader.

We also learnt how to read user input from the console using the BufferedReader and InputStreamReader, as well as how to change the input into the required data types.

I hope this article on example of bufferedreader in java with inputstreamreader helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪