Difference between revisions of "Java/C2/User-Input/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
Line 121: | Line 121: | ||
|- | |- | ||
| 02:04 | | 02:04 | ||
− | |So type, before the class '''import''' ''space'' '''java''' ''dot'' '''io '''''dot'' star semi colon. | + | |So type, before the class '''import''' ''space'' '''java''' ''dot'' '''io '''''dot'' '''star''' semi colon. |
|- | |- |
Revision as of 11:25, 27 August 2014
Time | Narration |
00:02 | Welcome to the spoken-tutorial on Taking user input in Java using BufferedReader . |
00:09 | In this tutorial we will learn: |
00:11 | To take user input in Java |
00:13 | About InputStreamReader and BufferedReader |
00:17 | To follow this tutorial you must know: |
00:19 | how to write, compile and run a simple java program in Eclipse |
00:24 | You must also know about the datatypes in Java |
00:27 | If not, please refer to the spoken tutorial on these topics available at spoken hyphen tutorial dot org.
|
00:35 | Here, I am using,
Ubuntu v 11.10 JDK 1.6 and Eclipse IDE 3.7.0 |
00:44 | Now, we will learn what a BufferedReader is! |
00:48 | It is a class in java which is used to read text from an input stream. |
00:53 | It provides efficient way to read array of characters and lines.
|
00:59 | To make use of BufferedReader , we need to import three classes' from the java dot io package. |
01:05 | These three classes are:
|
01:12 | We will learn about packages and how to import classes in the coming tutorials. |
01:18 | Now how is the input taken? |
01:21 | All the input that we take from the user will be in the form of String. |
01:26 | It has to be then typecasted or converted to the particular datatype. |
01:31 | We will see that while we write our program to take the user input. |
01:35 | Now, let us see the syntax to implement BufferedReader. |
01:39 | Once you import the three classes, you need to create an object of InputStreamReader. |
01:45 | You also need to create an object of BufferedReader. |
01:49 | We will learn about this in detail, when we write our program. |
01:54 | So, let us switch to Eclipse. |
01:56 | I have already opened a class named InputBufferedReader. |
02:00 | We will begin with importing the java.io package.
|
02:04 | So type, before the class import space java dot io dot star semi colon. |
02:13 | This will import the classes InputStreamReader, BufferedReader and IOException.
|
02:20 | Now We will make use of BufferedReader inside the main method. |
02:25 | We need to throw an IOException in whatever method we use the BufferedReader. |
02:31 | So right after the main method type throws space IOException. |
02:42 | Now, what does this mean? |
02:45 | Exceptions are errors which occur in Java when some unexpected circumstances occur. |
02:52 | To prevent Exception errors we make use of throws keyword. |
02:57 | Throws is a keyword which is used during Exception handling. |
03:00 | It is used whenever we know that Exception error will definitely occur. |
03:05 | When we use BufferedReader, exception error always take place. |
03:10 | To prevent Exception errors from taking place we make use of throws IOException. |
03:16 | We will learn about Exception Handling in the coming tutorials. |
03:20 | Now, we will create an object of InputStreamReader. |
03:24 | For that, inside the main method type InputStreamReader space isr equalto new space InputStreamReader parentheses. |
03:44 | Within parentheses, type System dot in and then semicolon. |
03:52 | InputStreamReader is a class in javawhich allows us to take the user input . |
04:01 | System dot intells the java compiler to take the input from the user using keyboard. |
04:10 | The input that System dot in takes is stored in the object of InputStreamReader for sometime. |
04:17 | After this we create an object of BufferedReader. |
04:22 | So type, BufferedReader br equal to new space BufferedReader and then parentheses. |
04:36 | Inside the parentheses, type the object of InputStreamReader which is isr. |
04:43 | Now, isr only helps to take the input from the user. |
04:48 | BufferedReader helps to store the value in the BufferedReader object. |
04:54 | Isr passes this value to the BufferedReader object to store it. |
05:01 | Now, let us start taking input from the user. |
05:06 | We will first ask the user to enter a String. So create a variable of String type
|
05:14 | Type, String space str semicolon |
05:19 | Now ask the user to enter his name. |
05:23 | So type, System dot out dot println within brackets and double quotes Enter your nameand thensemicolon. |
05:33 | To take the input as String we will type. |
05:37 | str equal to br dot readLine parentheses and then semicolon. |
05:45 | The readLine method will read the input from the user . |
05:51 | Now, let us take the input as an integer. Create a variable of typeint. |
06:01 | So type int n semicolon. |
06:05 | Ask the user to enter his age. |
06:08 | So Type, System dot out dot println within brackets and double quotes Enter your age semicolon.
|
06:21 | Also, create another variable named str1 of String type in order to take the input.
|
06:31 | Now to take the input as String, type str1 equal to br dot readLine parentheses and then semicolon. |
06:45 | To convert it into integer datatype, type n equal to Integer with capital I dot parseInt, capital I within brackets str1semicolon |
07:05 | Integer is a class and parseInt is its method. |
07:11 | This method converts the argument passed within the bracket into integer. |
07:18 | Now, let us display the output for name and age. |
07:22 | So, type System dot out dot println within brackets and double quotes The name is plus str semicolon. |
07:38 | Next line type, System dot out dot println The ages plus n and thensemicolon. |
07:50 | Now, Save the file, press Ctrl, Skeys. Now let us run the program |
07:55 | So press Control and F11 keys. |
08:00 | In the output, you are asked to Enter your name. |
08:03 | So type your name. I will type here Ramu, press Enter. |
08:08 | You will be asked to enter your age. |
08:11 | I will type here20 and then press Enter |
08:13 | We get the output as : |
08:15 | The name is Ramu |
08:16 | And The age is 20. |
08:18 | Thus we know how to take an input from the user. |
08:24 | In this tutorial we have learnt : |
08:26 | About InputStreamReader |
08:28 | About BufferedReader |
08:29 | And Converting from String to the desired datatype
|
08:33 | For self-assessment, take a float, byte and character input from the user and then display the output |
08:42 | Also take a number as input and divide it by 3 then Display the output on the console. |
08:49 | To know more about the spoken tutorial project;Watch the video available at the following link |
08:54 | It summarises the Spoken Tutorial project |
08:57 | If you do not have good bandwidth, you can download and watch it |
09:02 | The Spoken Tutorial Project Team |
09:04 | Conducts workshops using spoken tutorials |
09:07 | Gives certificates to those who pass an online test |
09:11 | For more details, please write to contact@spoken-tutorial.org |
09:18 | Spoken Tutorial Project is a part of the Talk to a Teacher project |
09:21 | It is supported by the National Mission on Education through ICT, MHRD, Government of India |
09:27 | More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
09:36 | This is Arya Ratish from IIT Bombay.
Thank You for joining. |