Fallowing icon

Wednesday, October 3, 2012

Java BufferedReader- Get user input


            
      In this lesson we learn how to get user input from user. The Last lesson we discussed how to get user input using Scanner class, now we discuss how to get user input using BufferedReader class. 

      BufferedReader class is already defined in java.io package. We want to import BufferedReader and InputStreamReader classes to create BufferedReader object.


Import BufferedReader and InputStreamReader Classes-        

       import java.io.BufferedReader;
                import java.io.InputStreamReader; 
               
                We use this import statements above the class body. BufferedReader and InputStreamReader classes are already defined in java.io package, So we can import this class. Then we can access this class for our requirements.
  

Create a BufferedReader object-

      Before we are learned how to create object. Now we create object using BufferedReader class.


               BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));

          
      BufferedReader bf is variable which reference data type. System.in is input stream it means that scanner will take input from console.


Structure of the BufferedReader -

       
                 import java.io.BufferedReader;
                 import java.io.IOException;
                 import java.io.InputStreamReader;       
                 class Test{           
                        public static void main(String args[]){
                               try{
                                       BufferedReader bf=new BufferedReader(new InputStreamReader(System.in);                         
                                       String StrValue=bf.readLine();
                                }catch(IOException e){System.out.println(e);}
                        }  
                  }


     First we import BufferedReader, InputStreamReader, IOException classes. IOException class is import for catch exceptions, We'll learn about this sectors in the exception handling    lesson
     Then we create BufferedReader object. This class is intended to about an InputStream, this is liked a bridge from byte streams to character streams. 
     readLine(); is the method in BufferedReader class. It's return the String type values.try catch is used for catch exceptions when the errors are occurring at run time.


Example-   

             import java.io.BufferedReader;
             import java.io.IOException;
             import java.io.InputStreamReader;

             class Test{
                   String marks1,marks2,marks3; 
                   String subject="Maths",name1,name2,name3;
                   double nMks1,nMks2,nMks3;
                   double max,min,sum,avg;
      
                    void getValue() throws IOException{
                            BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
          
                            System.out.print("Enter first name - ");
                            name1=bf.readLine();
          
                            System.out.print("Enter second name- ");
                            name2=bf.readLine();
          
                            System.out.print("Enter third name - ");
                            name3=bf.readLine();
          
                            System.out.print("Enter the "+name1+"'s "+subject+" marks -");
                            marks1=bf.readLine();
          
                            System.out.print("Enter the "+name2+"'s "+subject+" marks -");
                            marks2=bf.readLine();
          
                            System.out.print("Enter the "+name3+"'s "+subject+" marks -");
                            marks3=bf.readLine();
                      }      
                      void getSuccess(){
                            nMks1=Double.parseDouble(marks1);
                            nMks2=Double.parseDouble(marks2);
                            nMks3=Double.parseDouble(marks3);
                      }      
                      void cal(){          
                             sum=nMks1+nMks2+nMks3;
                             avg=sum/3;  
          
                             if(nMks1<nMks2){
                                 if(nMks2<nMks3){
                                        max=nMks3;
                                        min=nMks1;
                                  }                  
                                  else{
                                         max=nMks2;
                                         if(nMks1>nMks3)
                                                   min=nMks3;
                                          else
                                                   min=nMks1;          
                                    }          
                                }
                                else{
                                     if(nMks1<nMks3){
                                            max=nMks3;
                                            min=nMks2;
                                      }

                                      else{
                                             max=nMks1;
                                             if(nMks2>nMks3)
                                                      min=nMks3;
                                             else
                                              min=nMks2;
                                        }   
                                   }
                     }              
                     void printValues(){
                           System.out.println("\n\n\nName              Subject               Marks");
                           System.out.println(name1+"                "+subject+"                "+marks1);
                           System.out.println(name2+"                "+subject+"                "+marks2);
                           System.out.println(name3+"                "+subject+"                "+marks3);
                           System.out.println("\n");          
                           System.out.println("Maximum marks   ="+max);
                           System.out.println("Minimum marks   ="+min);
                           System.out.println("Summation marks ="+sum);
                           System.out.println("Average marks   ="+avg);        
                       }
      
                       public static void main(String[] args) {
                             Test a=new Test();

                              try {
                                       a.getValue();       
                                       a.getSuccess();
                                       a.cal();
                                       a.printValues();           
            
                                } catch (IOException e) {
                                       System.out.println(e.getMessage());
                                   }


                        }
                   }









Discussion-


getValue-

      We declared BufferedReader object and all input statements in this method. Input command is used six times in this method.  


getSuccess-

       We have used readLine();  method for get values via BufferedReader. readLine method is returned the values in String data type, But we can't do any mathematical operations via String data type. In this reason we convert this String data type values to double. 

ex:-
      VARIABLENAME=Double.parseDouble(STRINGVARIABLE); 


      We can use this code for Integers, floats and double data types. Replace int,float or double data type for the word VARIABLENAME and replace String data type variable name for the word STRINGVARIABLE, then you can get double,int or float data type via  String variable.


cal-  
       This method is used for get mathematical expressions.
We calculate maximum value, minimum value, summation and average using this method.


    

No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment