Friday, 16 February 2018

BufferedReader In Java

It allows reading input from the keyboard.
java.io package is used in BufferedReader Class
e.g:
import  java.io.*;
or
import  java.io.BufferedReader;
System.in(keyboard): It represents the standard input stream
System.out(monitor): It represents the standard output stream
read():It is used to read single character
readLine():It is used to read multiple character(string)

BufferedReader:

BufferedReader  reads a couple of characters from the specified stream and stores it in a buffer.Thats why its performance is faster.
It maintains a buffer and retrieving data from buffer is always fast as compared to retrieving data from disk.
InputStreamReader:
InputStreamReader reads only one character from specified stream and remaining characters still remain in the stream.
The two classes implement the same interface of Reader. When you will use just InputStreamReader  without  BufferedReader, you will get  poor performance.

InputStreamReader converts byte streams to character streams. It reads bytes and decodes them into characters using a specified charset.

Note:
We should use Buffered Reader because it takes less time to convert the data from bytes to character and it provides better performance.

throws:
throws is used to declare an exception.
throws is used in method signature.
 throws is followed by class
Syntax:

  return_type  method_name() throws exception_class_name
  {  
 //
   }  
e.g
void show() throws  IOException
{
//statement
}


Note:
Use throws IOException in every method signature like
Eg 1:
public static void main(String args[])throws IOException
    {
//statement
   }
Eg 2.
public void show() throws IOException
{
//statement
}

Methods used in  BufferedReader:

Integer.parseInt() 
It is used to convert  String to int.
int i=Integer.parseInt();  

Float.parseFloat()
It is used to convert String to float .
float f= Float.parseFloat(); 

Double.parseDouble()
It is used to convert  String to double .
double  d=Double.parseDouble(); 

Long.parseLong()
It is used to convert String to long.
long l=Long.parseLong();  

Boolean.parseBoolean() 
It is used to convert String to boolean.
boolean b=Boolean.parseBoolean();  

Steps to create Programs using BufferedReader:

Step1:
import  package like
import java.io.*;

Step 2:
Use  throws  IOException  in method signature(every method).

Step 3:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 Or
    InputStreamReader r=new InputStreamReader(System.in);    
    BufferedReader br=new BufferedReader(r); 

Step 4:
Convert the method like
int a=Integer.parseInt(br.readLine());
float b=Float.parseFloat(br.readLine());
double c=Double.parseDouble(br.readLine());
String name= br.readLine();
char ch=(char)br.read();

Step 5:
Display output

How to take int,string and double input from user in java using BufferedReader?

Example :
import java.io.*;
public class Simple {
   public static void main(String[] args)throws IOException {
       BufferedReader br = new BufferedReader(new   InputStreamReader(System.in));
       
        System.out.println("Enter name");
        String name=br.readLine();

        System.out.println("Enter  age");
        int age = Integer.parseInt(br.readLine());
       
      System.out.println("Enter marks");
        double marks = Double.parseDouble(br.readLine());

        System.out.printf("Student Information:- " + "name is:"+name+"\n"+
                          " age is" + age+"\n"+"marks is:"+marks);
    }
    }
Output:
Enter name
Dev
Enter  age
25
Enter marks
34.56
Student Information:-
name is:Dev
age is:25
marks is:34.56

How to take character input from user in java using BufferedReader?
import  java.io.*;
public class Simple {
 void show()throws IOException
 {
     BufferedReader br = new BufferedReader(new  InputStreamReader(System.in));
      
        System.out.println("Enter Gender(M/F)");
        char gender=(char)br.read();
        System.out.printf("Gender is:"+gender);
 }
     public static void main(String[] args)throws IOException {
      Simple s1=new Simple();
       s1.show();
    }
    }
Output:
Enter Gender(M/F)")
M
Gender is: M

Note:
readLine ()
It reads the string
String name=br.readLine();
read()
It reads the character
char ch=(char)br.read();


1 comment:

  1. Great read! exactly what i was looking for. Thankyou.

    ReplyDelete

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...