Reading Line By Line6:43 with Carling Kirk
We'll break apart our CSV file.
[MUSIC] 0:00 Now that we've got the ability to load data into our program, 0:04 we can focus on making it more usable. 0:08 So far, we've got an array of strings, one for each line of our CSV file. 0:11 We'll need to break each line apart so 0:16 that we get a separate element for each field of data in our CSV file. 0:18 We can create a class to hold the data from each soccer game result, 0:23 then we can take each of those elements and map them to fields in our class. 0:27 When you hear the word parse in programming, 0:32 it often refers to the act of translating a string into a more meaningful type. 0:34 So in our game result data, we'll be parsing the strings that hold values like 0:40 dates and numbers into the corresponding types that will create in our class, 0:44 this way we can perform calculations on our results. 0:49 Let's go! 0:52 We're going to create another method that's similar to a read file method, but 0:55 instead of returning the entire file contents, it's going to do something 0:58 similar to what we did right here and break it up by each line break. 1:02 We're going to approach it a little differently though. 1:06 Instead of reading the entire file and 1:08 then breaking it up, we're going to read it line by line. 1:10 Let's call it read soccer results and since we're just going to break up 1:14 the fields in the CSV file, it can return a list of strings. 1:19 public static List<string> ReadSoccerResults and 1:22 it'll take a string called fileName, 1:32 You might not have seen a list before, it's a lot like an array, 1:42 it holds multiple objects of a particular type. 1:45 I'm using it here instead of an array because a list doesn't have a fixed length 1:49 like an array does. 1:53 A list allows us to easily add items to it, you'll learn more about lists and 1:55 other courses here at Treehouse. 2:00 Inside this method we'll need our StreamReader again. 2:02 We'll also go ahead and instantiate a new list of strings and return it so 2:05 Visual Studio is happy with us and doesn't give us any red squiggly lines. 2:09 var soccerResults = new List<string>();, 2:12 and return soccerResults. 2:21 Now we need our StreamReader, using(var reader = new StreamReader), 2:30 and we'll pass it the (fileName) parameter. 2:37 Now we need an open and close curly brace. 2:43 On the reader object there's another method we can use called, 2:47 read line, the way this method works is pretty self-explanatory. 2:50 It reads the next line in the file and returns it. 2:54 It also moves the position in the file, so that when it's called again the next 2:57 line will be read, until it gets to the end of the file and then it returns nil. 3:01 In order to use this method, we're going to need a loop and 3:09 of course there's a couple of ways we can do this. 3:12 One way is to use the peek method, peek will look at the character in 3:15 the next position and return an integer based on its position. 3:20 So if a peek returns a negative one, we've reached the end of the file. 3:24 We can write a while loop with the peek method until it returns a negative one. 3:28 So while(reader.Peek() > -1), 3:32 then inside the loop we'll use the read 3:40 line method to read the next line in the file and add it to our list of strings. 3:44 string line = reader.ReadLine(), 3:48 and soccerResults.Add(line;). 3:57 We can take this one step further, though. 4:04 Each of our fields in our CSV file are separated by a comma character, 4:07 so we can use the string.split method to split it up .split, 4:12 And we'll pass it a comma character with our single quotes. 4:19 We'll need to change our string to a string array though, and 4:23 the return type will change. 4:26 Array of String, and 4:31 list of string array and the return type. 4:34 This method will evolve quite a bit as we continue to refine our application. 4:42 Every time our while loop repeats, the Peek method will look at the file, and 4:46 then our ReadLine method will look at the file a second time. 4:51 We can actually refactor this to use the ReadLine in our while condition so 4:55 that we only make one call to the file each time the while loop repeats. 5:00 So up here we'll need a string line, 5:06 and we'll take this reader.ReadLine out here, 5:13 while line = reader.ReadLine and 5:21 while that is != to null, we'll keep reading the file. 5:28 Then we'll need to change this name and 5:35 say a line.Split, instead of lines, let's call that values, 5:38 soccerResults.Add(values), that'll be much more efficient. 5:46 Let's see what it returns to us by setting a breakpoint here. 5:53 Then we'll go back up to Main. 5:57 We'll delete this stuff here and we'll call = 6:02 ReadSoccerReults and pass it the filename. 6:07 Now we can debug to hit our break point, F5. 6:15 Okay, hover over soccerResults and 6:21 looks like we've got our list of string arrays with values from our spreadsheet. 6:25 There's the header values and these have our actual values. 6:29 Next, we'll be creating a class for soccer game results and 6:36 exploring the different types we'll need to use. 6:40
You need to sign up for Treehouse in order to download course files.Sign up