1 00:00:00,510 --> 00:00:03,120 We can use scanner to make our applications interactive. 2 00:00:03,690 --> 00:00:07,440 We've already built many applications using the five main data types. 3 00:00:08,100 --> 00:00:15,630 Br view they are int to store whole numbers long for large real numbers, double for decimals, car 4 00:00:15,630 --> 00:00:18,060 for characters and string for text. 5 00:00:21,280 --> 00:00:25,450 But there is a small issue with the apps you've built so far, and that is that they've been static. 6 00:00:26,230 --> 00:00:29,380 In other words, you've been storing predefined values. 7 00:00:30,490 --> 00:00:35,080 Now in the real world, you want to make a dynamic application where values come from, the user. 8 00:00:35,710 --> 00:00:41,260 Like here, for example, the application is asking the user questions and the user answers by submitting 9 00:00:41,260 --> 00:00:42,430 values through the terminal. 10 00:00:44,050 --> 00:00:50,350 And once the user answers every question, the application responds, this application is fully interactive 11 00:00:50,350 --> 00:00:53,920 because the application and the user are communicating with each other. 12 00:00:58,640 --> 00:01:01,970 So in this lesson, you're going to use scanner to get values from the user. 13 00:01:05,690 --> 00:01:10,340 First thing I need you to do is create in your class, so inside the Section two project to make a new 14 00:01:10,340 --> 00:01:16,190 file named Survey Java and inside the survey class, make sure it has the main method. 15 00:01:23,010 --> 00:01:26,160 Scanner waits for the user to enter a value in the terminal. 16 00:01:29,390 --> 00:01:33,140 In the survey app, you will ask the user questions such as How old are you? 17 00:01:34,040 --> 00:01:37,220 And now Scanner needs to wait for the user to enter an integer. 18 00:01:37,790 --> 00:01:40,130 We do that with scanned next minute. 19 00:01:44,430 --> 00:01:48,420 And now the user must go to the terminal type a number and press enter. 20 00:01:49,390 --> 00:01:54,610 Once the user presses enter, scanned next and picks up the integer and stores it in the variable. 21 00:01:59,530 --> 00:02:01,870 We need to import scanner in order to use it. 22 00:02:02,680 --> 00:02:05,740 Scanner comes from a package called Java Util. 23 00:02:06,400 --> 00:02:09,340 So from Java, you tell we can import scanner. 24 00:02:10,520 --> 00:02:12,350 And notice that we import at the very top. 25 00:02:16,280 --> 00:02:23,900 So back to our code, we're going to import scanner, to the survey class import and from Java, you 26 00:02:23,900 --> 00:02:26,050 tell we're going to import scanner. 27 00:02:32,070 --> 00:02:34,470 Now we can access scanner inside the survey class. 28 00:02:34,590 --> 00:02:35,370 Simple as that. 29 00:02:36,270 --> 00:02:40,530 And so the next step is to set up scanners so that it can receive input from the user. 30 00:02:42,840 --> 00:02:46,440 So we need to make a scanner variable, and that involves three steps. 31 00:02:47,010 --> 00:02:53,070 The variable must be F-Type's scanner, the variable name I usually call it scan, but you can call 32 00:02:53,070 --> 00:02:59,550 it anything and you're going to set the variable equal to a new scanner such that this scanner can get 33 00:02:59,550 --> 00:03:01,950 input from the system system not in. 34 00:03:07,350 --> 00:03:07,950 Scanner. 35 00:03:09,570 --> 00:03:10,170 Skin. 36 00:03:12,010 --> 00:03:14,260 Is equal to a new scanner instance. 37 00:03:17,710 --> 00:03:21,430 That's going to receive input from the system system in. 38 00:03:25,670 --> 00:03:28,400 And now we're going to use scanner to get a values from the user. 39 00:03:31,050 --> 00:03:34,710 Scanner has different methods that we can leverage to get values from the user. 40 00:03:34,980 --> 00:03:36,690 And next line is one of them. 41 00:03:37,500 --> 00:03:40,310 Next line waits for the user to enter a string. 42 00:03:41,530 --> 00:03:44,080 So we're going to start by giving the user a warm welcome. 43 00:03:44,290 --> 00:03:48,250 So system died out the bright line. 44 00:03:50,790 --> 00:03:54,630 Welcome, thank you for taking the survey. 45 00:04:00,740 --> 00:04:01,940 Then we'll say system. 46 00:04:03,190 --> 00:04:06,100 Dot out dot print line. 47 00:04:06,880 --> 00:04:09,190 What is your name? 48 00:04:14,430 --> 00:04:14,880 All right. 49 00:04:14,910 --> 00:04:16,950 Let's compile the code and run this. 50 00:04:28,270 --> 00:04:32,530 As you can see, this only prints two messages, then the app terminates. 51 00:04:34,640 --> 00:04:40,010 We need to cull Skin Dot next line so that it can wait for the user to enter a stream value. 52 00:04:44,310 --> 00:04:45,390 All right, let's try it out. 53 00:04:46,840 --> 00:04:47,770 Compile your code. 54 00:04:48,210 --> 00:04:49,030 Run it. 55 00:04:50,140 --> 00:04:52,600 And check it out, it waits for me to enter a string. 56 00:04:52,630 --> 00:04:53,860 I'm going to right, Sam. 57 00:04:54,270 --> 00:04:55,090 And there you go. 58 00:04:57,440 --> 00:05:01,640 What's happening is that Scandal Next line waits for the user to enter a string. 59 00:05:01,880 --> 00:05:06,530 The user being me and the user went to the terminal and typed, Sam. 60 00:05:07,690 --> 00:05:11,410 And once the user presses enter scan, the next line picks up the value. 61 00:05:15,890 --> 00:05:20,060 But to be able to use this value in the future, we have to store it inside a variable. 62 00:05:21,050 --> 00:05:25,610 So what I'm going to do is say string name is equal to scandal. 63 00:05:25,700 --> 00:05:26,420 Next line. 64 00:05:27,550 --> 00:05:32,260 So now, when the user enters their answer, scanned, the next line gets the string of value and stores 65 00:05:32,260 --> 00:05:33,340 it in the variable name. 66 00:05:37,890 --> 00:05:40,480 Next, stubble is another method we can call from Skinner. 67 00:05:40,770 --> 00:05:43,230 This one waits for the user to enter a double value. 68 00:05:44,130 --> 00:05:47,220 So now we're going to ask the user how much money they would spend on coffee. 69 00:05:48,520 --> 00:05:51,820 So System died out die print line. 70 00:05:58,000 --> 00:06:00,430 Money, do you spend? 71 00:06:02,600 --> 00:06:03,680 On coffee. 72 00:06:04,870 --> 00:06:08,650 And now, Renee, you stand up next, double to wait for the user to enter a double. 73 00:06:11,530 --> 00:06:13,030 So we'll say double. 74 00:06:14,420 --> 00:06:15,530 Coffee price. 75 00:06:17,040 --> 00:06:19,770 Is equal to scan next double. 76 00:06:23,550 --> 00:06:25,500 All right, we're compiling our code. 77 00:06:27,310 --> 00:06:28,180 And running it. 78 00:06:29,490 --> 00:06:31,350 What is your name, Sam? 79 00:06:31,920 --> 00:06:33,840 How much money do you spend on coffee? 80 00:06:34,530 --> 00:06:35,940 Let's say a dollar fifty. 81 00:06:55,860 --> 00:07:00,030 So what happens is it picks up the value and stores it in a variable coffee price. 82 00:07:01,980 --> 00:07:07,440 First, the programs they use or how much money they spend on coffee scanned next level, wait for the 83 00:07:07,440 --> 00:07:08,760 user to enter a decimal. 84 00:07:09,390 --> 00:07:11,790 It picks it up and stores it in a variable. 85 00:07:16,470 --> 00:07:16,990 All right. 86 00:07:17,010 --> 00:07:23,550 For more practice, we're going to ask the user how much money they spend on fast food system dart out 87 00:07:23,710 --> 00:07:24,870 dart line. 88 00:07:26,930 --> 00:07:28,670 How much money? 89 00:07:31,670 --> 00:07:34,940 Do you spend on fast food? 90 00:07:38,920 --> 00:07:47,620 And you're going to store the user's answer in a variable double food price is equal to scan Dot next 91 00:07:47,620 --> 00:07:48,070 double. 92 00:08:00,350 --> 00:08:02,090 What is your name, Sam? 93 00:08:02,450 --> 00:08:04,820 How much money do you spend on coffee? 94 00:08:04,850 --> 00:08:06,140 A dollar fifty? 95 00:08:06,920 --> 00:08:11,150 How much money do you spend on fast food will say four dollars and fifty cents. 96 00:08:12,580 --> 00:08:16,510 And once again scanned next, I will wait for the user to enter a decimal. 97 00:08:16,810 --> 00:08:19,270 It picks it up and stores it in a variable. 98 00:08:22,620 --> 00:08:25,260 Next is another method you can call from scanner. 99 00:08:25,470 --> 00:08:29,640 And this one, as you could probably guess, it waits for the user to enter an integer. 100 00:08:32,669 --> 00:08:36,690 So to start ominous, these are how many times a week they buy coffee and how many times a week they 101 00:08:36,690 --> 00:08:37,559 buy fast food. 102 00:08:38,280 --> 00:08:41,970 So system dot out dot print line. 103 00:08:46,270 --> 00:08:49,570 How many times a week? 104 00:08:52,470 --> 00:08:54,210 Do you buy coffee? 105 00:09:01,360 --> 00:09:05,320 And I'm going to store the user's answers in a variable coffee amount. 106 00:09:08,500 --> 00:09:11,560 And we're going to set that equal to scan next. 107 00:09:11,590 --> 00:09:12,160 It. 108 00:09:13,590 --> 00:09:17,940 Then we'll do the same thing here, system dot out dot print line. 109 00:09:25,400 --> 00:09:30,590 How many times a week do you buy fast food? 110 00:09:37,450 --> 00:09:40,930 And we'll store the user's answer in a variable food amount. 111 00:09:47,760 --> 00:09:51,700 And we're going to get the user's answer by waiting for the next it's. 112 00:09:56,560 --> 00:09:56,980 All right. 113 00:09:57,110 --> 00:09:59,680 Recompile the code and run it. 114 00:10:08,150 --> 00:10:08,930 What is your name? 115 00:10:09,200 --> 00:10:09,740 Sam? 116 00:10:10,370 --> 00:10:12,260 How much money you spend on coffee, a dollar? 117 00:10:12,260 --> 00:10:15,290 Fifty four dollars and fifty cents? 118 00:10:15,770 --> 00:10:17,810 How many times a week do you buy coffee? 119 00:10:18,350 --> 00:10:18,950 Five. 120 00:10:19,370 --> 00:10:20,900 Fast food seven. 121 00:10:22,020 --> 00:10:22,380 All right. 122 00:10:22,410 --> 00:10:24,090 This app is shaping up pretty well. 123 00:10:24,870 --> 00:10:28,380 The first scandal, an accident, waits for the user to answer the key question. 124 00:10:28,980 --> 00:10:35,850 Then it picks up the value in stories that in the amount, the second scanned on and waits for the user 125 00:10:35,850 --> 00:10:37,590 to answer the fast food question. 126 00:10:38,340 --> 00:10:41,250 Then it picks up the value and stores it in food amounts. 127 00:10:44,060 --> 00:10:47,840 And now every variable stories is one of the users answers. 128 00:10:56,300 --> 00:10:59,030 You might be wondering what if the user puts a wrong value? 129 00:11:00,240 --> 00:11:03,390 And the second question, for example, let me just rerun my code. 130 00:11:13,400 --> 00:11:18,800 When I put in the first answer and now here, what if I put a string, when next they will expect a 131 00:11:18,800 --> 00:11:19,370 decimal? 132 00:11:20,410 --> 00:11:21,130 Let's try up. 133 00:11:23,270 --> 00:11:25,700 And Java throws an error and it crashes. 134 00:11:29,000 --> 00:11:35,360 Next, we'll expect to decimal notice drink, and so if you enter a string next double doesn't know 135 00:11:35,360 --> 00:11:36,110 what to do with it. 136 00:11:36,650 --> 00:11:38,390 So the application crashes. 137 00:11:44,320 --> 00:11:46,540 You should close the scanner when you're done with it. 138 00:11:47,290 --> 00:11:49,510 Keeping scanner open is a memory leak. 139 00:11:50,110 --> 00:11:53,530 When you're done with it, you can close it with scanned clubs. 140 00:11:55,270 --> 00:11:57,220 Scan Duclos. 141 00:12:02,480 --> 00:12:05,360 Now, if you scroll up notice that the warning is gone. 142 00:12:09,670 --> 00:12:13,900 Let's recap in this lesson, you learn to use scanner to get values from the user. 143 00:12:15,690 --> 00:12:18,390 Next line waits for the user to enter a string value. 144 00:12:19,050 --> 00:12:23,910 And once the user presses enter scanned, our next line picks up the string and stores it in the variable. 145 00:12:24,810 --> 00:12:30,480 Next double waits for the user to enter a double value, and once the user presses enter scan, the 146 00:12:30,480 --> 00:12:33,690 next double picks up the double and stores it in the variable. 147 00:12:35,490 --> 00:12:38,700 Next in, wait for the user to enter an into value. 148 00:12:39,480 --> 00:12:45,090 And once the user presses enter scanned next and picks up the end and stores it in the variable. 149 00:12:49,220 --> 00:12:51,320 Restore it every value that the user entered. 150 00:12:51,350 --> 00:12:52,640 So what do we do now? 151 00:12:52,910 --> 00:12:54,410 Stick around for part two.