[CSC 204] Connecting Data between Arrays

Andrew J. Pounds pounds_aj at mercer.edu
Sat Nov 15 07:30:09 EST 2014


Overnight I got a few emails asking me for help on how to connect the 
data between the array containing the stations to the array containing 
the state info.  Here is one way to do it...

Lets assume that you have done the following.   You have a complete 
array of all the stations; each element of this array is a Station 
objects (with instance variables for stationID and stateID that are both 
strings).

Using your stations array you have created an array of state 
information; each element of this array is stored as a unique State 
object (with instance variables for stateID,  the sum of the readings, 
and the number of readings.  While your array elements will have stateID 
information, the other two instance variables should be zero.  So at 
this point you have completed all of the work associated with building 
the arrays for your program.  You have one array that will serve as a 
LOOKUP TABLE and another array that will actually hold the data you process.

*Verify that you have all this done correctly before continuing!*

If you have gotten this far, you are now ready to read the daily.txt 
file and start populating the state array with temperature data.  The 
first thing you should do in this step is make sure that you can read 
the daily.txt file and extract the stationID and temperature from each 
line.  Once you know that you can do this correctly the proceed as follows:

 1. For each line of the daily text file read the stationID and the
    temperature
 2. By searching through each entry in your stations array, compare the
    stationID you just read from the daily.txt file with the information
    in your stations array to find the array index where the stationID
    is found.
 3. Once you know the index, you can extract the stateID into a variable
    with something like...

                     found = false;
                     index = 0;
                     state = "";
                     while ( !found && index < stations.length ) {
                          if ( stations[index].getID().equals(stationID) ) {
                              found = true;
                              state = stations[index].getState();
                          }
                          index++;
                     }

         where getID() and getState() are methods inside my Station class.

 4. Once you have used your LOOKUP TABLE to resolve the state name for
    the stationID, you now need to find the index of the location of the
    state in your array of states.  You will do something very similar
    to what I did above to locate the position of the state in the array
    of states and then, once you have the index of the location in the
    state array, call your method to add the temperature reading. 
    Something like..

stateData[index].addTemp(temp);

         Again, this assumes that in my State class I have an addTemp() 
method defined.

 5. Read the next line of the 400000+ lines in the daily.txt file and do
    the whole thing again...


As always, let me know where you get stuck.



-- 
Andrew J. Pounds, Ph.D.  (pounds_aj at mercer.edu)
Professor of Chemistry and Computer Science
Mercer University,  Macon, GA 31207   (478) 301-5627
http://faculty.mercer.edu/pounds_aj

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://theochem.mercer.edu/pipermail/csc204/attachments/20141115/69545dfe/attachment.html>


More information about the csc204 mailing list