Once again, we tie everything together by adding user input.
First it says to remove all the code inside Main, but leave the print statement.
Okay.
Sure.
Then we're going to create a store object and a card object.
But as class variables, that just means that we need to access these variables from more than one place
inside the class.
And Task two is to define a function named load items.
It takes one parameter the file name though we are going to load items from.
And the function is going to throw a file not found exception.
And before I do anything, I'm going to call this method so that we can keep testing it as we write
our code.
Since the method throws attacked exception.
I'm going to try to run the code.
And catch the exception if it occurs.
Printing its message.
Inside the function.
First thing we're going to do is load items from the file name.
We can create a new object of the file input stream class.
In Person two at the file name that we want to connect to.
And the file a petition constructor throws a file not found exception, which is a checked exception.
We're already throwing the exception from the method.
So whoever is calling it, they're going to have to catch the failure.
Next thing we need to do is read the data from the next file.
So after connecting to the file, we're going to create a new instance of the scanner class.
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 while loop to read all the data from the file that we're scanning.
This wire loop will keep running as long as the file has a next line.
So it should run seven times.
And during each run, we're going to pick up the entire line as a string.
String line is equal to scanned file the next line.
Okay.
So what to do with each line?
I wonder.
Hmm.
This is kind of tricky.
So, looking at the file, each item is separated by a semicolon.
And the fields and each item are separated by an equal sign.
The first thing we want to do is separate each item.
So what we're going to do is split the line every time there's a pair of semicolons.
Ultimately that's going to return an array of string values, which we're going to store in an array
called items.
I'm just going to add something random here so that I can add another break point after this one.
Okay.
And launch the debugger.
You can visualize the array and its stores three items, and we effectively split every item that was
separated by a semicolon.
The next thing we're going to do is split both fields in each string.
So we need to create a far loop that runs through the length of the array of items.
And for the sake of clarity, I'm going to rename the counter to J.
You'll see why later.
And I'll index each item separately.
Every item that we run through, I'm going to split it into multiple strings using the equal separator.
That's going to return an array of two string values, which we're going to store in an array called
Fields.
Add another two breakpoints and launch the debugger.
Here are the array stories.
Three strings that represent items.
I guess I'll have to add something random here so I can see the fields array.
Okay.
So inside the loop, we're splitting each string into two fields, and now we can use both fields to
create an object.
But going back to the store, the set item method expects to indexes row and column.
Which we also like to call Iron J, where I represents the RO and J represents the position of the item
in that row.
We already have the position of each item from the loop, but we don't have the row index.
So what I'm going to do is replace the wire loop with a four loop.
I'm going to say Forint is equal to zero.
And the loop is going to keep running as long as a scanner detects that there is a next line.
This might seem new to you, but it really isn't.
This is the condition that determines when the for a loop should stop running.
You can put any condition you want and this loop is going to keep running until this condition returns.
False.
And what's beautiful about this is now we have a counter that keeps track of every line, which is basically
the row index, as well as a counter that keeps track of every element in that row.
And now we can use both fields that we're extracting here and add item objects to the store will call
stalks set item and we're going to store the item in its appropriate arrow index I as well as its appropriate
spot in that row.
J And in that spot we can set a new object of the item class.
The element at Index zero represents the name.
And the element that index one represents the price.
But we will get an error because the constructor expects a double or a passing in a string.
Remember, we can use the familiar double pass double method to pass a double from the string that we're
picking up.
And that's really it.
Finally, don't forget to close scanner.
You can feel free to add a break point and launch another debugging session.
It splits the first line into three strings.
Each string represents an item, and the far loop splits each string into fields.
And we're using these two fields to create an object and add it to an appropriate spot in the store.
If you keep stepping through it, eventually it's going to populate every single spot in the store so
we can just start the debugger, run the code.
And beautiful.
I'm going to stop here and we'll cover the remaining tasks in the next video.