so as I mentioned in the last video its time for a challenge and a challenge is to rewrite
the static initialization block the you can see on screen now using the try with
resources that we've talked about and discussed in this section instead of the
try finally code that we got in here in place now so pause the
video and go away and convert this static initialization block into using try with resources
instead of try finally and
when you're ready to come back and check my solution start the video so pause the
video now
ok so how did you get on hopefully
you've got the try with resources working let's go through now and get it setup so
what we need to do first is obviously in locations.java and we're in this
static
initialization block we are going to delete this first line we no longer need it anymore instead what we are going to
do is put the code in the try block so we are going to come up here....
....
because we've done that in the previous line and we come down on catch
IOException we are gonna leave that in place but we are gonna delete the finally block now
because of course the close now happens automatically with the scanners as
we talked about in previous videos so that's that's the first one then the
next time we are going to read the exits we need to do a similar initialization
so we come up to the start here
.....I guess we could have
just copied the
line below...
...
....because that's now no longer needed because it's
automatically closed so we just run this to confirm that it still works
....ok so that is working ok so that is the challenge but just before I finished the video you may have
noticed that we actually stop needing a scanner to read the exits when I change
the code to read a line at a time and split it
so by removing the scanner its actually quite easy so what we can do is come up here to
line 57 and get rid of the scanner all together and instead make that....
...because of course
we are no longer using that then we set up a string so...
....
....we can delete the string input now because that has been change on line 59 again this is not part of the challenge so this is an optional extra step that I am showing at the end of the video so don't think that you have miss on a step I have told you to do in the challenge so its an extra section given
that weren't using scanner anymore it made sense to actually remove that we could have actually done
that at the same time as I stopped using the scanner for passing the data but the
BufferedReader class close method throws an unchecked exception so I would have had to
caught that in the finally block as well and that's the reason why I didn't do that
any earlier but now that would actually converted to the try with resources it's
a simple change to make alright so that's actually it that's the challenge
over I actually before I go lets just run up to make sure that it still does work obviously very important and lets try up down
go west up
the quit so that seems to be working ok this time I will see you in the next video