1 00:00:00,570 --> 00:00:01,140 In this video. 2 00:00:01,140 --> 00:00:03,090 We're going to cover the solution for part six. 3 00:00:03,090 --> 00:00:05,820 We're going to tie everything together by adding user interactivity. 4 00:00:06,480 --> 00:00:10,050 I'll start off by removing everything from Main except for the manager object. 5 00:00:17,840 --> 00:00:23,270 And Task one tells us to make the contact manager a class variable, which implies that we need to access 6 00:00:23,270 --> 00:00:25,550 it from more than one place inside the class. 7 00:00:25,790 --> 00:00:26,300 Fair enough. 8 00:00:27,700 --> 00:00:28,590 That's taken care of. 9 00:00:28,610 --> 00:00:31,490 Now we're going to define a function named load contacts. 10 00:00:35,480 --> 00:00:39,110 Takes one parameter, the file name that we're going to load contacts from. 11 00:00:45,230 --> 00:00:47,840 And the function throws a file not found exception. 12 00:00:55,670 --> 00:00:58,640 First thing we're going to do is load contact from the file name. 13 00:00:59,150 --> 00:01:01,490 We can use a file input stream to accomplish that. 14 00:01:01,640 --> 00:01:04,550 So create a new object of the file input stream class. 15 00:01:09,170 --> 00:01:12,050 And pass into it the file name that we want to connect to. 16 00:01:16,290 --> 00:01:19,410 And the final upstream constructor throws a found that found exception. 17 00:01:19,680 --> 00:01:20,880 It's a checked exception. 18 00:01:20,880 --> 00:01:26,220 So Java would normally force us to catch the exception, but we're already throwing the exception from 19 00:01:26,220 --> 00:01:26,700 the method. 20 00:01:26,700 --> 00:01:29,340 So whoever is calling it is going to handle the failure. 21 00:01:30,210 --> 00:01:33,090 The next thing we want to do is read the data from the contacts file. 22 00:01:37,000 --> 00:01:39,700 So after connecting to the file, you can read its data. 23 00:01:39,700 --> 00:01:42,640 Using scanner will create a new scanner object. 24 00:01:50,050 --> 00:01:54,820 And instead of receiving input from the system, it's going to receive input from the final input stream. 25 00:01:55,750 --> 00:01:59,050 And then we can use a wire loop to read all the data from the file that we're scanning. 26 00:01:59,320 --> 00:02:02,830 The while loop is going to keep running as long as the file has a next line. 27 00:02:14,530 --> 00:02:16,150 So it should run three times. 28 00:02:21,440 --> 00:02:25,040 And during each run, we're going to create a new contact object. 29 00:02:33,220 --> 00:02:37,720 And for each object we're going to grab the name, phone number and birthdate using scanned. 30 00:02:37,840 --> 00:02:38,350 Next. 31 00:02:51,220 --> 00:02:56,770 And so during its run, each scandal next is going to grab the next string delimited by whitespace. 32 00:02:57,610 --> 00:02:58,690 And here we get an error. 33 00:02:58,750 --> 00:03:02,230 That's because the contact constructor can throw a pass exception. 34 00:03:02,950 --> 00:03:05,080 Pass exception is a checked exception. 35 00:03:05,080 --> 00:03:09,400 So if you want to call this constructor, Java's going to force you to try to run the code. 36 00:03:14,430 --> 00:03:18,330 And if the code happens to fail, you need to catch the pirates exception. 37 00:03:21,150 --> 00:03:23,310 And we'll just print the message that comes out of it. 38 00:03:28,970 --> 00:03:34,460 And now we're going to add every contact that we create to the contact list manager, not add contact. 39 00:03:34,820 --> 00:03:35,540 Contact. 40 00:03:37,970 --> 00:03:40,730 And as always, don't forget to close scanner when you're done with it. 41 00:03:49,020 --> 00:03:51,330 And that wraps up the load contact function. 42 00:03:52,060 --> 00:03:55,380 We can go back to the main method we're going to call load contacts. 43 00:03:59,470 --> 00:04:00,970 And it's not auto completing. 44 00:04:02,120 --> 00:04:03,770 Oh, I forgot to make it static. 45 00:04:04,130 --> 00:04:06,460 Remember, we have to make everything static inside men. 46 00:04:06,500 --> 00:04:08,240 I'll explain why in the next section. 47 00:04:08,510 --> 00:04:10,790 Right now, don't worry too much about why we do it. 48 00:04:11,060 --> 00:04:12,680 More so that we need to do it. 49 00:04:13,830 --> 00:04:17,970 And the file reloading contacts from is going to be contact start text. 50 00:04:21,560 --> 00:04:26,450 Once again we get an error because the load contact method can throw a file not found exception. 51 00:04:26,990 --> 00:04:28,850 This once again is a checked exception. 52 00:04:29,150 --> 00:04:32,540 So if you want to call this method, job is going to force you to try to run the code. 53 00:04:34,640 --> 00:04:38,750 And if the code happens to fail, you need to catch the file not found exception. 54 00:04:44,400 --> 00:04:46,740 And here we'll just print the message that comes out of it. 55 00:04:52,070 --> 00:04:54,740 So here we're going to print contacts, load it. 56 00:04:56,710 --> 00:04:57,790 With two lines. 57 00:05:05,200 --> 00:05:08,860 And we're going to follow that up by printing a two string of the contact manager. 58 00:05:14,860 --> 00:05:16,970 I think it's time to test our function and we out how? 59 00:05:17,020 --> 00:05:18,040 Break point right here. 60 00:05:18,400 --> 00:05:19,450 Launch the debugger. 61 00:05:23,850 --> 00:05:28,290 Step into the function, I'll step over creating the file in the stream object. 62 00:05:33,190 --> 00:05:34,720 As well as the scatter object. 63 00:05:36,830 --> 00:05:39,530 The wire loop runs through every line in the contacts file. 64 00:05:41,660 --> 00:05:46,730 And from each line we're creating a contact object from every string value that we're scanning using 65 00:05:46,730 --> 00:05:47,120 scanned. 66 00:05:47,120 --> 00:05:47,810 Up next. 67 00:05:55,510 --> 00:05:58,270 And then we're adding that contact to the contact list. 68 00:06:00,630 --> 00:06:04,410 And you'll notice that once we get to GM, her birthdate is invalid. 69 00:06:04,830 --> 00:06:07,440 So for Geo, a past exception is going to be thrown. 70 00:06:10,890 --> 00:06:14,580 But don't worry, the app is not going to crash because we're catching that exception. 71 00:06:21,620 --> 00:06:23,840 And for Thomas, everything should work fine. 72 00:06:33,990 --> 00:06:36,960 Now we have two valid contacts in the contact list. 73 00:06:49,270 --> 00:06:51,160 That's all for tasks one, two and three. 74 00:06:51,580 --> 00:06:54,010 In the next video we're going to implement task for.