Difference between revisions of "Java/C2/User-Input/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(8 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | || ''Time''' | + | || '''Time''' |
|| '''Narration''' | || '''Narration''' | ||
|- | |- | ||
| 00:02 | | 00:02 | ||
− | | Welcome to the spoken-tutorial on ''' | + | | Welcome to the spoken-tutorial on taking '''User input''' in '''Java''', using '''BufferedReader''' . |
|- | |- | ||
| 00:09 | | 00:09 | ||
− | | In this tutorial we will learn: | + | | In this tutorial, we will learn: |
|- | |- | ||
Line 17: | Line 17: | ||
|- | |- | ||
| 00:13 | | 00:13 | ||
− | | About InputStreamReader and BufferedReader | + | |About InputStreamReader and BufferedReader. |
− | + | ||
− | + | ||
|- | |- | ||
| 00:17 | | 00:17 | ||
− | | To follow this tutorial you must know | + | | To follow this tutorial, you must know |
|- | |- | ||
| 00:19 | | 00:19 | ||
− | |how to write, compile and run a simple java program in '''Eclipse''' | + | |how to write, compile and run a simple java program in '''Eclipse'''. |
|- | |- | ||
| 00:24 | | 00:24 | ||
− | | You must also know | + | | You must also know about the '''data types''' in '''Java'''. |
|- | |- | ||
| 00:27 | | 00:27 | ||
− | | If not, please refer to the '''spoken tutorial''' on these topics available at '''spoken | + | | If not, please refer to the '''spoken tutorial''' on these topics available at '''spoken''' hyphen '''tutorial''' dot '''org.''' |
− | + | ||
− | + | ||
|- | |- | ||
| 00:35 | | 00:35 | ||
− | | Here, I am using | + | | Here, I am using: |
− | + | ||
'''Ubuntu v 11.10''' | '''Ubuntu v 11.10''' | ||
− | |||
'''JDK 1.6''' and | '''JDK 1.6''' and | ||
− | + | '''Eclipse IDE 3.7.0''' | |
− | '''Eclipse IDE | + | |
|- | |- | ||
Line 53: | Line 46: | ||
|- | |- | ||
| 00:48 | | 00:48 | ||
− | |It is a '''class''' which is used to read text from an input stream. | + | |It is a '''class''' in java which is used to read text from an '''input stream'''. |
|- | |- | ||
| 00:53 | | 00:53 | ||
− | |It provides efficient way to read array of characters and lines. | + | |It provides efficient way to read '''array''' of '''characters''' and '''lines'''. |
− | + | ||
|- | |- | ||
|00:59 | |00:59 | ||
− | | To make use of ''BufferedReader | + | | To make use of ''BufferedReader'' , we need to '''import''' three classes from the '''java '''dot''' io package.''' |
|- | |- | ||
| 01:05 | | 01:05 | ||
|These three '''classes''' are: | |These three '''classes''' are: | ||
− | + | '''IOException''' | |
− | + | '''InputStreamReader''' and | |
− | + | '''BufferedReader''' | |
− | + | ||
|- | |- | ||
Line 82: | Line 73: | ||
|- | |- | ||
| 01:21 | | 01:21 | ||
− | | All the input that we take from the user will be in the form of ''' | + | | All the input that we take from the user will be in the form of '''string'''. |
|- | |- | ||
| 01:26 | | 01:26 | ||
− | |It has to be then typecasted or converted to the particular | + | |It has to be then typecasted or converted to the particular data type. |
|- | |- | ||
Line 98: | Line 89: | ||
|- | |- | ||
| 01:39 | | 01:39 | ||
− | |Once you import the three '''classes | + | |Once you import the three '''classes''', you need to create an object of '''InputStreamReader'''. |
|- | |- | ||
| 01:45 | | 01:45 | ||
− | |You also need to create an '''object''' of ''' | + | |You also need to create an '''object''' of ''' BufferedReader'''. |
|- | |- | ||
| 01:49 | | 01:49 | ||
− | |We will learn about this in detail | + | |We will learn about this in detail when we write our program. |
|- | |- | ||
Line 119: | Line 110: | ||
| 02:00 | | 02:00 | ||
|We will begin with importing the '''java.io package'''. | |We will begin with importing the '''java.io package'''. | ||
− | |||
|- | |- | ||
| 02:04 | | 02:04 | ||
− | |So type | + | |So, type: before the '''class''', '''import''' space '''java''' dot '''io ''' dot '''star''' semicolon. |
|- | |- | ||
− | | 02: | + | | 02:13 |
|This will import the classes '''InputStreamReader, BufferedReader and IOException.''' | |This will import the classes '''InputStreamReader, BufferedReader and IOException.''' | ||
− | |||
|- | |- | ||
| 02:20 | | 02:20 | ||
− | | Now | + | | Now, we will make use of '''BufferedReader '''inside the '''main method'''. |
|- | |- | ||
| 02:25 | | 02:25 | ||
− | |We need to throw an '''IOException''' in whatever method we use the '''BufferedReader'''. | + | |We need to '''throw''' an '''IOException''' in whatever '''method''' we use the '''BufferedReader'''. |
|- | |- | ||
| 02:31 | | 02:31 | ||
− | |So right after the main method type '''throws IOException.''' | + | |So, right after the '''main method''' type: '''throws space IOException.''' |
|- | |- | ||
Line 148: | Line 137: | ||
|- | |- | ||
| 02:45 | | 02:45 | ||
− | |'''Exceptions '''are errors which occur in Java when some unexpected circumstances occur. | + | |'''Exceptions '''are '''errors''' which occur in Java when some unexpected circumstances occur. |
|- | |- | ||
Line 164: | Line 153: | ||
|- | |- | ||
| 03:05 | | 03:05 | ||
− | |When we use ''' | + | |When we use ''BufferedReader'', exception error always takes place. |
|- | |- | ||
Line 172: | Line 161: | ||
|- | |- | ||
| 03:16 | | 03:16 | ||
− | |We will learn about | + | |We will learn about ''Exception Handling'' in the coming tutorials. |
|- | |- | ||
Line 180: | Line 169: | ||
|- | |- | ||
| 03:24 | | 03:24 | ||
− | |For that, inside the main method type '''InputStreamReader | + | |For that, inside the '''main method''' type: '''InputStreamReader''' space '''isr''' equalto '''new''' space '''InputStreamReader''' parentheses. |
|- | |- | ||
| 03:44 | | 03:44 | ||
− | | Within parentheses, type '''System | + | | Within parentheses, type: '''System''' dot '''in '''and then semicolon. |
|- | |- | ||
| 03:52 | | 03:52 | ||
− | |'''InputStreamReader '''is a '''class''' in '''java'''which allows us to take the user input . | + | |'''InputStreamReader '''is a '''class''' in '''java''' which allows us to take the '''user''' input . |
|- | |- | ||
| 04:01 | | 04:01 | ||
− | |'''System | + | |'''System '''dot''' in''' tells the''' java compiler''' to take the '''input''' from the user, using keyboard. |
|- | |- | ||
| 04:10 | | 04:10 | ||
− | |The input that '''System | + | |The input that '''System ''' dot '''in '''takes is stored in the '''object''' of '''InputStreamReader '''for sometime'''.''' |
|- | |- | ||
Line 204: | Line 193: | ||
|- | |- | ||
| 04:22 | | 04:22 | ||
− | |So | + | |So, type: '''BufferedReader space br''' equal to '''new''' space '''BufferedReader''' and then parentheses. |
|- | |- | ||
Line 220: | Line 209: | ||
|- | |- | ||
| 04:54 | | 04:54 | ||
− | |'''Isr '''passes this value to the '''BufferedReader | + | |'''Isr '''passes this value to the '''BufferedReader''' object to store it. |
|- | |- | ||
Line 228: | Line 217: | ||
|- | |- | ||
| 05:06 | | 05:06 | ||
− | |We will first ask the user to enter a '''String'''. So create a variable of '''String | + | |We will first ask the user to enter a '''String'''. So create a variable of '''String''' type. |
− | + | ||
|- | |- | ||
| 05:14 | | 05:14 | ||
− | |Type | + | |Type: '''String''' space '''str''' semicolon. |
|- | |- | ||
Line 241: | Line 229: | ||
|- | |- | ||
| 05:23 | | 05:23 | ||
− | |So | + | |So, type: '''System '''dot '''out '''dot '''println''' within brackets and double quotes '''Enter your name''' and then semicolon. |
|- | |- | ||
| 05:33 | | 05:33 | ||
− | | To take the input as '''String''' we will type | + | | To take the input as '''String''' we will type: |
|- | |- | ||
| 05:37 | | 05:37 | ||
− | | '''str | + | | '''str''' equal to '''br''' dot '''readLine''' parentheses and then semicolon. |
|- | |- | ||
| 05:45 | | 05:45 | ||
− | |The readLine method will read the input from the user | + | |The '''readLine''' '''method''' will read the input from the user. |
|- | |- | ||
| 05:51 | | 05:51 | ||
− | |Now, let us take the input as an '''integer'''. Create a variable of type'''int'''. | + | |Now, let us take the input as an '''integer'''. Create a variable of type '''int'''. |
|- | |- | ||
| 06:01 | | 06:01 | ||
− | |So type''' int n | + | |So, type: ''' int n ''' semicolon. |
|- | |- | ||
Line 268: | Line 256: | ||
|- | |- | ||
| 06:08 | | 06:08 | ||
− | | So | + | | So, type: '''System''' dot '''out''' dot'''println''' within brackets and double quotes ''' Enter your age''' semicolon. |
− | + | ||
|- | |- | ||
| 06:21 | | 06:21 | ||
|Also, create another variable named '''str1''' of '''String''' type in order to take the input. | |Also, create another variable named '''str1''' of '''String''' type in order to take the input. | ||
− | |||
|- | |- | ||
| 06:31 | | 06:31 | ||
− | |Now to take the input as '''String,''' type '''str1 | + | |Now, to take the input as '''String,''' type: '''str1 '''equal to '''br''' dot '''readLine''' parentheses and then semicolon. |
|- | |- | ||
| 06:45 | | 06:45 | ||
− | |To convert it into integer datatype, type '''n | + | |To convert it into integer datatype, type: '''n''' equal to '''Integer''' with capital I dot '''parseInt''', capital I within brackets '''str1''' semicolon. |
|- | |- | ||
| 07:05 | | 07:05 | ||
− | |Integer is a class and parseInt is its method. | + | |Integer is a '''class''' and '''parseInt''' is its '''method'''. |
|- | |- | ||
Line 298: | Line 284: | ||
|- | |- | ||
| 07:22 | | 07:22 | ||
− | |So, type '''System | + | |So, type: '''System''' dot '''out''' dot ''println'' within brackets and double quotes '''The name''' '''is''' plus '''str''' semicolon. |
|- | |- | ||
| 07:38 | | 07:38 | ||
− | |Next line | + | |Next line, type: '''System''' dot '''out''' dot '''println''' '''The age is''' plus '''n''' and then semicolon. |
|- | |- | ||
| 07:50 | | 07:50 | ||
− | | Now, '''Save''' the file, press '''Ctrl, | + | | Now, '''Save''' the file, press '''Ctrl, S''' keys. Now let us '''run''' the program. |
|- | |- | ||
− | | | + | | 07:55 |
− | |So press''' Control''' and '''F11''' keys. | + | |So, press''' Control''' and '''F11''' keys. |
|- | |- | ||
− | | | + | | 08:00 |
− | | In the output, you are asked to | + | | In the output, you are asked to enter your name. |
|- | |- | ||
| 08:03 | | 08:03 | ||
− | |So type your name. I will type here '''Ramu''' press '''Enter'''. | + | |So, type your name. I will type here '''Ramu''', press '''Enter'''. |
|- | |- | ||
Line 326: | Line 312: | ||
|- | |- | ||
| 08:11 | | 08:11 | ||
− | | I will type here'''20''' and then press '''Enter''' | + | | I will type here '''20''' and then press '''Enter'''. |
|- | |- | ||
Line 334: | Line 320: | ||
|- | |- | ||
| 08:15 | | 08:15 | ||
− | |'''The name is Ramu''' | + | |'''The name is Ramu''' And ''' The age is 20'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 08:18 | | 08:18 | ||
− | |Thus we know how to take an input from the user. | + | |Thus, we know how to take an input from the user. |
|- | |- | ||
Line 353: | Line 335: | ||
|- | |- | ||
| 08:28 | | 08:28 | ||
− | |About''' BufferedReader''' | + | | About''' BufferedReader''' And Converting from '''String''' to the desired data type. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 08:33 | | 08:33 | ||
− | |For self-assessment, take a float, byte and character input from the user and then display the output | + | |For self-assessment, take a '''float''', '''byte''' and '''character''' input from the user and then display the output. |
|- | |- | ||
| 08:42 | | 08:42 | ||
− | |Also take a number as input and divide it by 3 | + | |Also take a number as input and divide it by 3. Then display the output on the console. |
|- | |- | ||
| 08:49 | | 08:49 | ||
− | | To know more about the spoken tutorial project | + | | To know more about the spoken tutorial project, watch the video available at the following link. |
|- | |- | ||
| 08:54 | | 08:54 | ||
− | |It | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 08:57 | | 08:57 | ||
− | |If you do not have good bandwidth, you can download and watch it | + | |If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
| 09:02 | | 09:02 | ||
− | | The Spoken Tutorial | + | | The Spoken Tutorial project team: |
|- | |- | ||
| 09:04 | | 09:04 | ||
− | |Conducts workshops using spoken tutorials | + | |Conducts workshops using spoken tutorials. |
|- | |- | ||
| 09:07 | | 09:07 | ||
− | |Gives certificates to those who pass an online test | + | |Gives certificates to those who pass an online test. |
|- | |- | ||
Line 398: | Line 375: | ||
|- | |- | ||
| 09:18 | | 09:18 | ||
− | |Spoken Tutorial | + | |Spoken Tutorial project is a part of the Talk to a Teacher project. |
|- | |- | ||
| 09:21 | | 09:21 | ||
− | |It is supported by the National Mission on Education through ICT, MHRD, Government of India | + | |It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
| 09:27 | | 09:27 | ||
− | |More information on this | + | |More information on this mission is available at:http://spoken-tutorial.org\NMEICT-Intro. |
|- | |- |
Latest revision as of 15:37, 28 March 2017
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 data types 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:
IOException InputStreamReader and BufferedReader |
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 data type. |
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 semicolon. |
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 takes 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 java which allows us to take the user input . |
04:01 | System dot in tells 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 space 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 name and then semicolon. |
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 type int. |
06:01 | So, type: int n semicolon. |
06:05 | Ask the user to enter his age. |
06:08 | So, type: System dot out dotprintln 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 str1 semicolon. |
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 age is plus n and then semicolon. |
07:50 | Now, Save the file, press Ctrl, S keys. 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 here 20 and then press Enter. |
08:13 | We get the output as : |
08:15 | The name is Ramu 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 And Converting from String to the desired data type. |
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 summarizes 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. |