Tuesday, July 12, 2011

How to take user input in java


This program tell you how to take user input using java programing.
you only have to do add two line in program one is[InputStreamReader ir=new InputStreamReader(System.in);] which tell the java compiler that user is providing input from keybord, and 2nd line is [BufferedReader br=new BufferedReader(ir);]
hear java program take the input of user as buffer or in other word take in buffer memory.
In both line 6 and 7 we make object of InputStreamReader and BufferedReader method which are pre defined in java compiler.
We have to make object of each method to instantiate in a program, or in other word
if you want to use method in java user define or pre define you must have to make object of that methat.
      
source code
  1. import java.io.*;
  2. class user_input_two_no_for_sum
  3. {
  4.     public static void main(String[] args)throws IOException
  5.     {
  6.         InputStreamReader ir=new InputStreamReader(System.in);
  7.         BufferedReader br=new BufferedReader(ir);
  8.         int no1,no2,sum;
  9.         System.out.println("Enter no1=");
  10.         no1=Integer.parseInt(br.readLine());
  11.         System.out.println("Enter no2=");
  12.         no2=Integer.parseInt(br.readLine());
  13.         sum=no1+no2;
  14.             System.out.println("Total="+sum);
  15.             }
  16. }

No comments:

Post a Comment