Review of Guided Program ReadIn

Your Guided Program stepped you through an example of code that reads a list of of names from a datafile and stores the data in an array.

Bring up your code to follow along with the comments.

  1. The first section of our code has the declarations.

    The square brackets indicate that the variable names is an array. It will reference a list of objects, all of which will be of class String. The object inputName will be used to read in a name. Remember the data types - names is an array of String, inputName is a String. They do not match! Each element in the array is of class String, so inputName and any element in the array will have matching data types.

    We have decided that our program can handle up to 5 names, so we allocate the array to be size 5. In the rest of our code, we should not use the integer constant 5 when we mean the size of the array. What should we use? 

    What does the object called i represent? What does the object called numNames represent?

    • i represents an index, and is the commonly-used name of the variable in a for loop.
    • numNames represents the number of names that have been read in and stored in the array.
    • It represents the number of elements we are actually using in our partially filled array.
    • Notice that it starts out as 0 because we don't have any elements in our array.
    • Sometimes during this program i and numNames have the same value, but remember that they represent different things!

  2. In the next section, we open the datafile and read the lines of data. If an argument is not passed in on the command-line, it defaults to names.txt.

    This code demonstrates the basic algorithm for reading from a datafile:

    1. open the file
    2. read the data
    3. close the file

  3. Note that the loop to read in the data is the "standard structure of a while loop that gets input".

  4. As each name is read in, it is stored in the array.

  5. What if the array if full? Our code checks if there is room to store the new value, and if not, it prints a message and ends the input loop with a break.

    Notice that the if statement checks if there is room in the array by comparing numNames, which is currently 0, to names.length, which is 5. Remember that i is the index and numNames is how many names are in the array.

  6. If there is room, we allocate the String using the read-in value and store it in the array. In this case, we used the new even though we don't have to with String data, to remind ourselves that we would be required to use new and call a constructor for values that are not primitive.

    If we had an array of integers called numbers, what would the assignment statement look like?

    • Since int is a primitive data type, we would not use the new. We would first have to convert the read-in string value to an integer (let's say in a variable called n) and write the assignment like this:
          numbers[i] = n;
      

  7. Once all the data has been read, the input loop ends, and we close the file (very important).

  8. Next our code processes the array in order to print it.

  9. We have a for loop that starts at 0. Notice we use numNames for the logical expression - this is because numNames represents the number of names we have in the array.

    What would happen if we used names.length here instead?

    If you draw the code, you can see what happens.

One important aspect of this ReadIn code is that both the input loop and the for loop that prints the array stop at exactly the right place.

If either went through one too few iterations, some data would not be processed.

If either went one too many iterations, we'd be processing information that we aren't using (and probably end up with a NullPointerException or ArrayIndexOutOfBoundsException).

In order to test this code properly, we need to test our boundaries and any likely error conditions.

Suppose we want our code to work with up to 500 names. Once we change the code to make the array allocate 500 slots instead of 5, will this code still work?