Java/C2/User-Input/English
Title of script: Taking user input using BufferedReader
Author: Arya Ratish
Keywords: BufferedReader, InputStreamReader, user input, Video tutorial.
|
|
---|---|
Slide 1 | Welcome to the spoken-tutorial on Taking user input using BufferedReader in Java. |
Slide 2 | In this tutorial we will learn:
|
Slide 3 | To follow this tutorial you must know:
|
Slide 4 | Here, I am using,
Ubuntu 11.04 JDK 1.6 Eclipse 3.7.0 |
Slide 5 | Now, we will learn what a BufferedReader is.
It is a class which is used to read text from an input stream. It provides efficient way to read array of characters and lines.
|
Slide 6 | To make use of this, we need to import three classes from the java dot io package.
These three classes are:
We will learn about packages and how to import classes in the coming tutorials. |
Slide 6 | All the input that we take from the user will be in the form of String.
It has to be then typecasted or converted to the particular datatype. We will see that while we write our program to take the user input. |
Slide 7 | Now, let us see the syntax to implement BufferedReader.
Once you import the three classes, you need to create an object of BufferedReader. You also need to create an object of InputStreamReader. We will learn about this in detail when we write our program. |
Switch to Eclipse
import java.io.InputStreamReader; import java.io.BufferedReader;
|
So, let us switch to Eclipse.
I have already opened a class named InputBufferedReader. We will begin with importing the java.io package.
This will import the classes InputStreamReader, BufferedReader and IOException.
|
Type
public static void main(String[] args) throws IOException{
|
We will make use of BufferedReader inside the main method.
We need to throw an IOException in whatever method we use the BufferedReader. So right after the main method type throws IOException. Now, what does this mean? Exceptions are errors which occur in Java when some unexpected circumstances occur. To prevent Exception errors we make use of throws keyword. Throws is a keyword which is used during Exception handling. It is used whenever we know that Exception error will definitely occur. When we use BufferedReader, exception error always take place. To prevent Exception errors from taking place we make use of throws IOException. We will learn about Exception Handling in the coming tutorials. |
Inside main method,
|
Now, we will create an object of InputStreamReader.
For that, type InputStreamReader space isr equalto new space InputStreamReader and parentheses. Inside the parentheses, type System dot in and then semicolon. InputStreamReader is a class which allows us to take input from the user. System dot in tells the java compiler to take input from the user using keyboard. The input that System dot in takes is stored in the object of InputStreamReader for sometime. After this we create an object of BufferedReader. So type, BufferedReader br equal to new space BufferedReader and then parentheses. Inside the parentheses, type the object of InputStreamReader which is isr. Now, isr only helps to take the input from the user. BufferedReader helps to store the value in the BufferedReader object. Isr passes this value to the BufferedReader object to store it. |
Type,
String str; int n; String str1; System.out.println(“Enter your name”); str=br.readLine(); System.out.println(“Enter your age”);
|
Now, let us start taking input from the user.
We will first ask the user to enter a String. Ask the user to enter the name. So type, System dot out dot println within brackets and double quotes Enter your name. Before this we will create a variable of String type. So type, String space str. To take the input as String, we will type str equal to br dot readLine and parentheses,then semicolon. The readLine method will read the input from user . Now, let us take the input as an integer. Ask the user to enter the age. Type, System dot out dot println within brackets and double quotes Enter your age. Create a variable of type int. So type int n semicolon. Also, create another variable named str1 of String type in order to take the input. So type, String str1 semicolon. To take the input as String, type str1 equal to br dot readLine and parentheses, then semicolon. To convert it into integer datatype, type n equal to Integer dot parseInt within brackets str1. Integer is a class and parseInt is its method. This method converts the argument passed within the bracket into integer. Now, let us display the output for the name and age. So, type System dot out dot println within brackets and double quotes The name is plus str semicolon. Next line type, System dot out dot println within brackets and double quotes The age is plus n semicolon. |
Now, let us Save, Compile and Run this code.
So press Control and F11 keys simultaneously. | |
Enter your name
Arya Enter your age 24 The name is Arya. The age is 24 |
In the output, you are asked to Enter your name.
Type your name. Then press Enter. Enter your age. Press Enter again. We get the output as follows: The name is Arya. The age is 24. This is how we take input from the user. |
Summary | In this tutorial we have learnt :
|
Assignment | For self-assessment, take a float, byte and character input from the user and display them on the console.
Take a number as input and divide it by 3. Display the output on the console. |
Slide 9
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial About the Spoken Tutorial Project |
Watch the video available at the link shown below
It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Slide 10
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact@spoken-tutorial.org |
Slide 11
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
This is Arya Ratish from IIT Bombay.
Thank You for joining. |