In this video.
We're going to cover the solution for part six.
We're going to tie everything together by adding user interactivity.
I'll start off by removing everything from Main except for the manager object.
And Task one tells us to make the contact manager a class variable, which implies that we need to access
it from more than one place inside the class.
Fair enough.
That's taken care of.
Now we're going to define a function named load contacts.
Takes one parameter, the file name that we're going to load contacts from.
And the function throws a file not found exception.
First thing we're going to do is load contact from the file name.
We can use a file input stream to accomplish that.
So create a new object of the file input stream class.
And pass into it the file name that we want to connect to.
And the final upstream constructor throws a found that found exception.
It's a checked exception.
So Java would normally force us to catch the exception, but we're already throwing the exception from
the method.
So whoever is calling it is going to handle the failure.
The next thing we want to do is read the data from the contacts file.
So after connecting to the file, you can read its data.
Using scanner will create a new scanner object.
And instead of receiving input from the system, it's going to receive input from the final input stream.
And then we can use a wire loop to read all the data from the file that we're scanning.
The while loop is going to keep running as long as the file has a next line.
So it should run three times.
And during each run, we're going to create a new contact object.
And for each object we're going to grab the name, phone number and birthdate using scanned.
Next.
And so during its run, each scandal next is going to grab the next string delimited by whitespace.
And here we get an error.
That's because the contact constructor can throw a pass exception.
Pass exception is a checked exception.
So if you want to call this constructor, Java's going to force you to try to run the code.
And if the code happens to fail, you need to catch the pirates exception.
And we'll just print the message that comes out of it.
And now we're going to add every contact that we create to the contact list manager, not add contact.
Contact.
And as always, don't forget to close scanner when you're done with it.
And that wraps up the load contact function.
We can go back to the main method we're going to call load contacts.
And it's not auto completing.
Oh, I forgot to make it static.
Remember, we have to make everything static inside men.
I'll explain why in the next section.
Right now, don't worry too much about why we do it.
More so that we need to do it.
And the file reloading contacts from is going to be contact start text.
Once again we get an error because the load contact method can throw a file not found exception.
This once again is a checked exception.
So if you want to call this method, job is going to force you to try to run the code.
And if the code happens to fail, you need to catch the file not found exception.
And here we'll just print the message that comes out of it.
So here we're going to print contacts, load it.
With two lines.
And we're going to follow that up by printing a two string of the contact manager.
I think it's time to test our function and we out how?
Break point right here.
Launch the debugger.
Step into the function, I'll step over creating the file in the stream object.
As well as the scatter object.
The wire loop runs through every line in the contacts file.
And from each line we're creating a contact object from every string value that we're scanning using
scanned.
Up next.
And then we're adding that contact to the contact list.
And you'll notice that once we get to GM, her birthdate is invalid.
So for Geo, a past exception is going to be thrown.
But don't worry, the app is not going to crash because we're catching that exception.
And for Thomas, everything should work fine.
Now we have two valid contacts in the contact list.
That's all for tasks one, two and three.
In the next video we're going to implement task for.