0

BufferedReader and Scanner tutorial

bufferedreader and the Scanner are the two most important classes in java that can be used read user inputs, files and can be used for a lot other purposes. BufferedReader class extends Reader whereas the Scanner class extends Object .The BufferedReader class has methods to read a line being inputted by the user , skip bytes ,close the stream being used …etc and Scanner class have the methods to read integers ,double ,float ,next line, long ..etc .Today we’ll see a tutorial to read user input through these two classes .The below code will prompt the user to enter numbers ,read them and initialize the inputted number to an integer ,float variable ,long variable and display it to the user

Java bufferedreader and Java scanner Example:-

/*
* Tutorial to read user inputs with BufferedReader and Scanner Classes
* Copyright:-Pramod Choudhari//techwayz.com
*/
import java.io.*;
import java.util.*;
publicclassBufferedReaderExample {
publicstaticvoid main(String args[])
{
int i=0; //initializing the intvariable
float f=0F;//initializing the float variable
long l=0; //initializing the long variable
Scanner ss=null; //initializing the variable
try{
BufferedReader buf =new BufferedReader(newInputStreamReader(System.in)); //creating the buffered reader object
System.out.println(“Enter an integer”); //prompting user to enter an integer
i=Integer.parseInt(String.valueOf(buf.readLine())); //reading the string input and converting it to an int in real time
System.out.println(“Entered integer is:”+i);//displaying the integer
System.out.println(“Enter a floating point number..”);//prompting user to enter a floating point number
f=Float.parseFloat(buf.readLine());//reading the string input and converting it to a float
System.out.println(“Entered floating point number is:”+f); //Displaying the floating point variable
System.out.println(“Enter a long number”);//prompting user to enter a long variable
l=Long.valueOf(buf.readLine()); //reading the string input and converting it to long value
System.out.println(“Entered long number is:”+l);//displaying the long value
System.out.println(“Entered Number is:”+i);//displaying the 1st entered number
System.out.println(“Now lets try out the scanner…Kindly Enter an integer number”); //prompting user to enter the second number
ss=new Scanner(System.in); //creating a Scanner object to prevent NullPointer exception
i=ss.nextInt(); //reading the Integer being inputted by the user
System.out.println(“Scanner Read the Integer:”+i); //Displaying the second number to the user
System.out.println(“Kindly Enter a floating point number”); //prompting user to enter the second number
f=ss.nextFloat(); //reading the float being inputted by the user
System.out.println(“Scanner Read the floating point number:”+f); //Displaying the second number to the user
System.out.println(“Kindly Enter a long number”); //prompting user to enter the second number
l=ss.nextLong(); //reading the long number being inputted by the user
System.out.println(“Scanner Read the long variable:”+l); //Displaying the second number to the user
}catch(IOException x)
{
x.printStackTrace(); //This line would throw any exceptions caught while processing user inputs
}
}
}

 

I hope you’ve found this little tutorial useful .

Pramod

I hold a post graduate degree in Computer Sciences . I've 1+ year of industry experience as a Java/J2EE developer.My passions include writing applications , exploring different technologies ,Traveling .

Leave a Reply

Your email address will not be published. Required fields are marked *


3 + eight =

CommentLuv badge