1 00:00:00,450 --> 00:00:03,090 Once again, we tie everything together by adding user input. 2 00:00:03,810 --> 00:00:07,650 First it says to remove all the code inside Main, but leave the print statement. 3 00:00:08,189 --> 00:00:08,580 Okay. 4 00:00:08,590 --> 00:00:09,000 Sure. 5 00:00:14,750 --> 00:00:17,420 Then we're going to create a store object and a card object. 6 00:00:17,420 --> 00:00:22,700 But as class variables, that just means that we need to access these variables from more than one place 7 00:00:22,700 --> 00:00:23,720 inside the class. 8 00:00:42,730 --> 00:00:46,150 And Task two is to define a function named load items. 9 00:00:53,180 --> 00:00:57,020 It takes one parameter the file name though we are going to load items from. 10 00:01:02,490 --> 00:01:05,400 And the function is going to throw a file not found exception. 11 00:01:09,080 --> 00:01:13,130 And before I do anything, I'm going to call this method so that we can keep testing it as we write 12 00:01:13,130 --> 00:01:13,760 our code. 13 00:01:28,490 --> 00:01:30,440 Since the method throws attacked exception. 14 00:01:30,440 --> 00:01:32,000 I'm going to try to run the code. 15 00:01:37,780 --> 00:01:40,060 And catch the exception if it occurs. 16 00:01:46,600 --> 00:01:47,800 Printing its message. 17 00:01:53,380 --> 00:01:54,340 Inside the function. 18 00:01:54,340 --> 00:01:57,160 First thing we're going to do is load items from the file name. 19 00:01:57,310 --> 00:02:00,130 We can create a new object of the file input stream class. 20 00:02:07,810 --> 00:02:10,720 In Person two at the file name that we want to connect to. 21 00:02:11,830 --> 00:02:17,140 And the file a petition constructor throws a file not found exception, which is a checked exception. 22 00:02:17,620 --> 00:02:19,600 We're already throwing the exception from the method. 23 00:02:19,600 --> 00:02:22,420 So whoever is calling it, they're going to have to catch the failure. 24 00:02:24,220 --> 00:02:26,980 Next thing we need to do is read the data from the next file. 25 00:02:27,160 --> 00:02:31,480 So after connecting to the file, we're going to create a new instance of the scanner class. 26 00:02:35,640 --> 00:02:40,080 And instead of receiving input from the system, it's going to receive input from the final input stream. 27 00:02:41,680 --> 00:02:45,370 And then we can use a while loop to read all the data from the file that we're scanning. 28 00:02:45,940 --> 00:02:49,360 This wire loop will keep running as long as the file has a next line. 29 00:02:55,990 --> 00:02:57,720 So it should run seven times. 30 00:02:58,240 --> 00:03:01,420 And during each run, we're going to pick up the entire line as a string. 31 00:03:04,700 --> 00:03:07,490 String line is equal to scanned file the next line. 32 00:03:12,890 --> 00:03:13,160 Okay. 33 00:03:13,160 --> 00:03:14,390 So what to do with each line? 34 00:03:14,510 --> 00:03:15,290 I wonder. 35 00:03:15,860 --> 00:03:16,100 Hmm. 36 00:03:16,730 --> 00:03:17,660 This is kind of tricky. 37 00:03:18,320 --> 00:03:21,890 So, looking at the file, each item is separated by a semicolon. 38 00:03:27,160 --> 00:03:30,790 And the fields and each item are separated by an equal sign. 39 00:03:34,790 --> 00:03:37,490 The first thing we want to do is separate each item. 40 00:03:39,910 --> 00:03:44,470 So what we're going to do is split the line every time there's a pair of semicolons. 41 00:03:51,190 --> 00:03:55,630 Ultimately that's going to return an array of string values, which we're going to store in an array 42 00:03:55,630 --> 00:03:56,620 called items. 43 00:04:00,990 --> 00:04:05,190 I'm just going to add something random here so that I can add another break point after this one. 44 00:04:06,980 --> 00:04:07,400 Okay. 45 00:04:07,400 --> 00:04:08,510 And launch the debugger. 46 00:04:14,440 --> 00:04:20,350 You can visualize the array and its stores three items, and we effectively split every item that was 47 00:04:20,350 --> 00:04:21,850 separated by a semicolon. 48 00:04:24,020 --> 00:04:27,320 The next thing we're going to do is split both fields in each string. 49 00:04:33,420 --> 00:04:37,440 So we need to create a far loop that runs through the length of the array of items. 50 00:04:44,180 --> 00:04:47,570 And for the sake of clarity, I'm going to rename the counter to J. 51 00:04:47,810 --> 00:04:48,860 You'll see why later. 52 00:04:54,540 --> 00:04:56,550 And I'll index each item separately. 53 00:05:01,110 --> 00:05:06,420 Every item that we run through, I'm going to split it into multiple strings using the equal separator. 54 00:05:11,180 --> 00:05:15,620 That's going to return an array of two string values, which we're going to store in an array called 55 00:05:15,620 --> 00:05:16,310 Fields. 56 00:05:22,080 --> 00:05:24,780 Add another two breakpoints and launch the debugger. 57 00:05:39,050 --> 00:05:40,460 Here are the array stories. 58 00:05:40,490 --> 00:05:42,590 Three strings that represent items. 59 00:05:48,430 --> 00:05:51,860 I guess I'll have to add something random here so I can see the fields array. 60 00:06:07,200 --> 00:06:07,360 Okay. 61 00:06:07,410 --> 00:06:12,780 So inside the loop, we're splitting each string into two fields, and now we can use both fields to 62 00:06:12,780 --> 00:06:13,860 create an object. 63 00:06:28,350 --> 00:06:33,600 But going back to the store, the set item method expects to indexes row and column. 64 00:06:35,140 --> 00:06:41,740 Which we also like to call Iron J, where I represents the RO and J represents the position of the item 65 00:06:41,740 --> 00:06:42,430 in that row. 66 00:06:43,180 --> 00:06:47,920 We already have the position of each item from the loop, but we don't have the row index. 67 00:06:51,340 --> 00:06:54,550 So what I'm going to do is replace the wire loop with a four loop. 68 00:06:57,320 --> 00:06:59,740 I'm going to say Forint is equal to zero. 69 00:07:05,340 --> 00:07:09,900 And the loop is going to keep running as long as a scanner detects that there is a next line. 70 00:07:10,620 --> 00:07:12,990 This might seem new to you, but it really isn't. 71 00:07:13,350 --> 00:07:17,340 This is the condition that determines when the for a loop should stop running. 72 00:07:17,760 --> 00:07:23,040 You can put any condition you want and this loop is going to keep running until this condition returns. 73 00:07:23,040 --> 00:07:23,580 False. 74 00:07:34,930 --> 00:07:40,120 And what's beautiful about this is now we have a counter that keeps track of every line, which is basically 75 00:07:40,120 --> 00:07:45,040 the row index, as well as a counter that keeps track of every element in that row. 76 00:07:46,080 --> 00:07:52,440 And now we can use both fields that we're extracting here and add item objects to the store will call 77 00:07:52,440 --> 00:07:59,850 stalks set item and we're going to store the item in its appropriate arrow index I as well as its appropriate 78 00:07:59,850 --> 00:08:01,140 spot in that row. 79 00:08:01,200 --> 00:08:06,450 J And in that spot we can set a new object of the item class. 80 00:08:09,580 --> 00:08:11,980 The element at Index zero represents the name. 81 00:08:14,800 --> 00:08:17,710 And the element that index one represents the price. 82 00:08:19,620 --> 00:08:24,870 But we will get an error because the constructor expects a double or a passing in a string. 83 00:08:26,720 --> 00:08:32,390 Remember, we can use the familiar double pass double method to pass a double from the string that we're 84 00:08:32,390 --> 00:08:33,020 picking up. 85 00:08:37,580 --> 00:08:38,570 And that's really it. 86 00:08:43,230 --> 00:08:45,030 Finally, don't forget to close scanner. 87 00:08:51,890 --> 00:08:55,250 You can feel free to add a break point and launch another debugging session. 88 00:09:05,730 --> 00:09:08,190 It splits the first line into three strings. 89 00:09:08,520 --> 00:09:13,950 Each string represents an item, and the far loop splits each string into fields. 90 00:09:14,370 --> 00:09:19,230 And we're using these two fields to create an object and add it to an appropriate spot in the store. 91 00:09:20,010 --> 00:09:25,200 If you keep stepping through it, eventually it's going to populate every single spot in the store so 92 00:09:25,200 --> 00:09:27,960 we can just start the debugger, run the code. 93 00:09:39,640 --> 00:09:40,720 And beautiful. 94 00:09:41,780 --> 00:09:45,380 I'm going to stop here and we'll cover the remaining tasks in the next video.